Search the Community
Showing results for tags 'k/d'.
-
so i tried to make saving system that saves kills and deaths but it doesn't save full script local root = getRootElement() local scoresRoot = getResourceRootElement(getThisResource()) local scoreColumns = {"Kills", "Deaths", "ratio", "status"} local isColumnActive = {} local KDR_DECIMAL_PLACES = 2 --http://lua-users.org/wiki/SimpleRound local function round(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult + 0.5) / mult end local function setScoreData (element, column, data) if isColumnActive[column] then setElementData(element, column, data) end end local dbConnection = dbConnect("sqlite", "backupexprank.db") local qh = dbQuery( dbConnection,"CREATE TABLE IF NOT EXISTS scores (name text,Kills text,Deaths text)") dbFree( qh ) function saveScore(sourcePlayer) local account = getPlayerAccount(sourcePlayer) local name = getAccountName(account) local Kills = getAccountData(account, "Kills") local Deaths = getAccountData(account, "Deaths") local qh = dbQuery( dbConnection, "SELECT * FROM scores where name=?",name) local res = dbPoll(qh,-1) dbFree( qh ) if #res > 0 then dbExec( dbConnection, "UPDATE scores SET Deaths=? where name=? ", Deaths,name ) dbExec( dbConnection, "UPDATE scores SET Kills=? where name=? ", Kills,name ) outputChatBox ( "Saved account " .. name .. " with the Kills " .. Kills .. " with the Deaths" .. Deaths .. " to our database") else dbExec(dbConnection, "INSERT INTO scores VALUES(?, ?, ?)", name, Kills, Deaths) outputChatBox ( "Saved account " .. name .. " with the Kills " .. Kills .. " with the Deaths" .. Deaths .. " to our database") end end addCommandHandler("savestats", saveScore) local function resetScores (element) setScoreData(element, "Kills", 0) setScoreData(element, "Deaths", 0) local status = "" if isPedDead(element) then status = "Dead" end setScoreData(element, "status", status) end local function updateRatio (element) local Deaths = getElementData(element, "Deaths") if Deaths == 0 then setScoreData(element, "ratio", "-") else local kdr = round(getElementData(element, "Kills") / Deaths, KDR_DECIMAL_PLACES) setScoreData(element, "ratio", tostring(kdr)) end end function updateActiveColumns () for i, column in ipairs(scoreColumns) do if get(column) then isColumnActive[column] = true exports.scoreboard:addScoreboardColumn(column) elseif isColumnActive[column] then isColumnActive[column] = false exports.scoreboard:removeScoreboardColumn(column) end end end addEventHandler("onResourceStart", scoresRoot, function () updateActiveColumns() for i, player in ipairs(getElementsByType("player")) do resetScores(player) end end ) addEventHandler("onResourceStop", scoresRoot, function () for i, column in ipairs(scoreColumns) do if isColumnActive[column] then exports.scoreboard:removeScoreboardColumn(column) end end end ) addEventHandler("onPlayerJoin", root, function () resetScores(source) end ) addEventHandler("onPlayerWasted", root, function (ammo, killer, weapon) if killer then if killer ~= source then -- killer killed victim setScoreData(killer, "Kills", getElementData(killer, "Kills") + 1) setScoreData(source, "Deaths", getElementData(source, "Deaths") + 1) if isColumnActive["ratio"] then updateRatio(killer) updateRatio(source) end else -- victim killed himself setScoreData(source, "self", getElementData(source, "self") + 1) end else -- victim died setScoreData(source, "Deaths", getElementData(source, "Deaths") + 1) if isColumnActive["ratio"] then updateRatio(source) end end setScoreData(source, "status", "Dead") end ) addEventHandler("onPlayerSpawn", root, function () setScoreData(source, "status", "") end ) i need that it would save always without typing command like player died save to db