xeon17 Posted May 15, 2014 Share Posted May 15, 2014 I need help on my script , i alerdy created a scores script it stoped working and on forum nobody helped me to fix it. I wanna change the MTA Scores script that it saves kills,deaths & ratio after player quit or server restart. local root = getRootElement() local scoresRoot = getResourceRootElement(getThisResource()) local scoreColumns = {"kills", "deaths", "self", "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 function resetScores (element) setScoreData(element, "kills", 0) setScoreData(element, "deaths", 0) setScoreData(element, "self", 0) setScoreData(element, "ratio", "-") 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 ) addCommandHandler("score", function (player) if player then for i, column in ipairs(scoreColumns) do if column == "status" then break end if isColumnActive[column] then exports.scoreboard:addScoreboardColumn(column) outputConsole(column .. ": " .. getElementData(player, column), player) end end end end ) i need help with setAccountData. Link to comment
justn Posted May 15, 2014 Share Posted May 15, 2014 function onQuit () local account = getPlayerAccount(source) if account and not (isGuestAccount (account)) then local kills = getElementData(source, "kills") local deaths = getElementData(source,"deaths") local self = getElementData(source,"self") local ratio = getElementData(source,"ratio") setAccountData (account, "kills", kills ) setAccountData (account, "deaths", deaths ) setAccountData (account, "self", self ) setAccountData (account, "ratio", ratio ) end end addEventHandler ("onPlayerQuit", getRootElement(), onQuit) function onLogin () local acc = getPlayerAccount(source) local kills = getAccountData(acc, "kills") local deaths = getAccountData(acc, "deaths") local self = getAccountData(acc, "self") local ratio = getAccountData(acc, "ratio") setElementData(source, "kills", kills) setElementData(source, "deaths", deaths) setElementData(source, "self", self) setElementData(source, "ratio", ratio) end addEventHandler("onPlayerLogin", getRootElement(), onLogin) Link to comment
xeon17 Posted May 16, 2014 Author Share Posted May 16, 2014 now script tottaly no work ( i changed a little too ) local root = getRootElement() local scoresRoot = getResourceRootElement(getThisResource()) local scoreColumns = {"kills", "deaths", "self", "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 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("onResourceStop", scoresRoot, function () for i, column in ipairs(scoreColumns) do if isColumnActive[column] then exports.scoreboard:removeScoreboardColumn(column) end end 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 ) addCommandHandler("score", function (player) if player then for i, column in ipairs(scoreColumns) do if column == "status" then break end if isColumnActive[column] then exports.scoreboard:addScoreboardColumn(column) outputConsole(column .. ": " .. getElementData(player, column), player) end end end end ) function onQuit () local account = getPlayerAccount(source) if account and not (isGuestAccount (account)) then local kills = getElementData(source, "kills") local deaths = getElementData(source,"deaths") local self = getElementData(source,"self") local ratio = getElementData(source,"ratio") setAccountData (account, "kills", kills ) setAccountData (account, "deaths", deaths ) setAccountData (account, "self", self ) setAccountData (account, "ratio", ratio ) end end addEventHandler ("onPlayerQuit", getRootElement(), onQuit) function onLogin () local acc = getPlayerAccount(source) local kills = getAccountData(acc, "kills") local deaths = getAccountData(acc, "deaths") local self = getAccountData(acc, "self") local ratio = getAccountData(acc, "ratio") setElementData(source, "kills", kills) setElementData(source, "deaths", deaths) setElementData(source, "self", self) setElementData(source, "ratio", ratio) end addEventHandler("onPlayerLogin", getRootElement(), onLogin) Link to comment
xeon17 Posted May 16, 2014 Author Share Posted May 16, 2014 With this script : scores save but they restart after resource stop.. i tried to fix but failed.. help please local root = getRootElement() local scoresRoot = getResourceRootElement(getThisResource()) local scoreColumns = {"kills", "deaths", "self", "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 function resetScores (element) setScoreData(element, "kills", 0) setScoreData(element, "deaths", 0) setScoreData(element, "self", 0) setScoreData(element, "ratio", "-") 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 ) addCommandHandler("score", function (player) if player then for i, column in ipairs(scoreColumns) do if column == "status" then break end if isColumnActive[column] then exports.scoreboard:addScoreboardColumn(column) outputConsole(column .. ": " .. getElementData(player, column), player) end end end end ) function onQuit () local account = getPlayerAccount(source) if account and not (isGuestAccount (account)) then local kills = getElementData(source, "kills") local deaths = getElementData(source,"deaths") local self = getElementData(source,"self") local ratio = getElementData(source,"ratio") setAccountData (account, "kills", kills ) setAccountData (account, "deaths", deaths ) setAccountData (account, "self", self ) setAccountData (account, "ratio", ratio ) end end addEventHandler ("onPlayerQuit", getRootElement(), onQuit) function onLogin () local acc = getPlayerAccount(source) local kills = getAccountData(acc, "kills") local deaths = getAccountData(acc, "deaths") local self = getAccountData(acc, "self") local ratio = getAccountData(acc, "ratio") setElementData(source, "kills", kills) setElementData(source, "deaths", deaths) setElementData(source, "self", self) setElementData(source, "ratio", ratio) end addEventHandler("onPlayerLogin", getRootElement(), onLogin) Link to comment
xeon17 Posted May 16, 2014 Author Share Posted May 16, 2014 +1 problem ,when a new player enter he no have kills/deaths/ratio on scoreboard , its emty.. i need to restart the resource that he get it.. anyone ? i tried to fix by my self . Link to comment
Castillo Posted May 18, 2014 Share Posted May 18, 2014 In this piece of code: addEventHandler("onResourceStart", scoresRoot, function () updateActiveColumns() for i, player in ipairs(getElementsByType("player")) do resetScores(player) end end ) This part of the script resets the scores when the resource starts, you can remove it to stop them from resetting. for i, player in ipairs(getElementsByType("player")) do resetScores(player) end Link to comment
xeon17 Posted May 18, 2014 Author Share Posted May 18, 2014 i alerdy tried it , but the script stoped working. i think i did something wrong. Link to comment
xeon17 Posted May 18, 2014 Author Share Posted May 18, 2014 and there is +1 problem, when a new player login, his kills/deaths/ratio on scoreboard are emty Link to comment
Castillo Posted May 18, 2014 Share Posted May 18, 2014 Post the script after removing it. And remember to check debugscript. Link to comment
xeon17 Posted May 18, 2014 Author Share Posted May 18, 2014 Post the script after removing it. And remember to check debugscript. take a eye on post 3 in this topic, the code i posted. Link to comment
Castillo Posted May 18, 2014 Share Posted May 18, 2014 Ah, that's because you removed the entire function, you must only remove the player loop to reset scores. Link to comment
xeon17 Posted May 18, 2014 Author Share Posted May 18, 2014 my computer have problems to start. i'll try it and answer in the topic. i'm now on my phone Link to comment
xeon17 Posted May 19, 2014 Author Share Posted May 19, 2014 my computer have problems to start. i'll try it and answer in the topic. i'm now on my phone I did what you said and fixed the problem , when the script restart scores no reset.. and i fixed the bug with emty kills/deaths/ratio when a new player enter serer.. thanks for your help the script work perfect now. Link to comment
xeon17 Posted June 11, 2014 Author Share Posted June 11, 2014 Scores no save when server restart , then everyone have 0 kills,0deaths0,ratio. Help local root = getRootElement() local scoresRoot = getResourceRootElement(getThisResource()) local scoreColumns = {"kills", "deaths", "self", "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 function resetScores (element) setScoreData(element, "kills", 0) setScoreData(element, "deaths", 0) setScoreData(element, "self", 0) setScoreData(element, "ratio", "-") 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() 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 ) addCommandHandler("score", function (player) if player then for i, column in ipairs(scoreColumns) do if column == "status" then break end if isColumnActive[column] then exports.scoreboard:addScoreboardColumn(column) outputConsole(column .. ": " .. getElementData(player, column), player) end end end end ) function onQuit () local account = getPlayerAccount(source) if account and not (isGuestAccount (account)) then local kills = getElementData(source, "kills") local deaths = getElementData(source,"deaths") local self = getElementData(source,"self") local ratio = getElementData(source,"ratio") setAccountData (account, "kills", kills ) setAccountData (account, "deaths", deaths ) setAccountData (account, "self", self ) setAccountData (account, "ratio", ratio ) end end addEventHandler ("onPlayerQuit", getRootElement(), onQuit) function onLogin () local acc = getPlayerAccount(source) local kills = getAccountData(acc, "kills") local deaths = getAccountData(acc, "deaths") local self = getAccountData(acc, "self") local ratio = getAccountData(acc, "ratio") if ( kills ) and (deaths ) and ( ratio ) then setElementData(source, "kills", kills) setElementData(source, "deaths", deaths) setElementData(source, "self", self) setElementData(source, "ratio", ratio) else setElementData(source, "kills", 0) setElementData(source, "deaths", 0) setElementData(source, "ratio", 0) end end addEventHandler("onPlayerLogin", getRootElement(), onLogin) Link to comment
Cadell Posted June 11, 2014 Share Posted June 11, 2014 Store score data in sql and use it after server restart all status will be safe there Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now