Kazafka Posted January 4, 2020 Share Posted January 4, 2020 OK. I was re-writing this script second time, after I wrote it at first. I wanted to add new feature. Then I've broke the script and I had to re-write it 2 times. Even second time didn't helped me. If you, community can help me, please, do whatever you want with these commands (addCommandHandlers) to make them work. Please. Here's the full script code IT'S IN POLISH, TRANSLATE IT A LITTLE BIT: --Tabele playersT = {} moneyT = {} --Funkcja zczytująca zapisane tabele addEventHandler("onResourceStart", root, function() local xml = xmlLoadFile("accounts.xml") -- if xml then local xml = xmlLoadFile("accounts.xml") -- local plrsBrnch = xmlFindChild(xml, "players", 0) local mnBrnch = xmlFindChild(xml, "money", 0) -- local plrsTable = xmlNodeGetChildren(plrsBrnch) local mnTable = xmlNodeGetChildren(mnBrnch) -- for i, node in ipairs(plrsTable) do playersT[i] = xmlNodeGetAttribute(node, "name") end -- for i, node in ipairs(mnTable) do moneyT[i] = xmlNodeGetAttribute(node, "value") end -- xmlUnloadFile(xml) else local xml = xmlCreateFile("accounts.xml", "main") -- local plrsBrnch = xmlCreateChild(xml, "players") local mnBrnch = xmlCreateChild(xml, "money") -- xmlSaveFile(xml) xmlUnloadFile(xml) end end) --Użyteczne 2 funkcje function table.size(tab) local length = 0 for _ in pairs(tab) do length = length + 1 end return length end function table.find(tabl,word) if type(tabl) ~= "table" or word == nil then return false else local ret = false for k,v in pairs(tabl) do if v == word then return k end end end end -- --2 funkcje zapisujące tabele addEventHandler("onResourceStop", root, function() local xml = xmlCreateFile("accounts.xml", "main") -- local plrsBrnch = xmlCreateChild(xml, "players") local mnBrnch = xmlCreateChild(xml, "money") -- for k, value in ipairs(playersT) do local plrsTable = xmlCreateChild(plrsBrnch, "object") xmlNodeSetAttribute(plrsTable, "name", value) end -- for k, value in ipairs(moneyT) do local mnTable = xmlCreateChild(mnBrnch, "object") xmlNodeSetAttribute(mnTable, "value", value) end -- xmlSaveFile(xml) xmlUnloadFile(xml) end) addEventHandler("onPlayerQuit", root, function() local xml = xmlCreateFile("accounts.xml", "main") -- local plrsBrnch = xmlCreateChild(xml, "players") local mnBrnch = xmlCreateChild(xml, "money") -- for k, value in ipairs(playersT) do local plrsTable = xmlCreateChild(plrsBrnch, "object") xmlNodeSetAttribute(plrsTable, "name", value) end -- for k, value in ipairs(moneyT) do local mnTable = xmlCreateChild(mnBrnch, "object") xmlNodeSetAttribute(mnTable, "value", value) end -- xmlSaveFile(xml) xmlUnloadFile(xml) end) -- --Reszta skryptu (komendy) --Funkcja do przelewów addCommandHandler("przelew", function(player, command, pname, amount) for k, v in ipairs(playersT) do if(string.match(getPlayerName(player), v)) then if pname and (getPlayerFromName(pname) ~= player) then if amount and (tonumber(amount) <= tonumber(getPlayerMoney(player))) then takePlayerMoney(player, tonumber(amount)) givePlayerMoney(getPlayerFromName(pname), tonumber(amount)) outputChatBox ("#09ff00Przelew ukończony pomyślnie!", player, 255, 255, 255, true) outputChatBox ("#ffffff" .. getPlayerName(player) .. "#09ff00 przelał Ci #ffffff$" .. amount .. "#09ff00 do twojego portfela!", getPlayerFromName(pname), 255, 255, 255, true) end end end end end) --Funkcja do wpłacania pieniędzy addCommandHandler("wplac", function(player, command, amount) for k, v in ipairs(playersT) do if(string.match(getPlayerName(player), v)) then if amount and (amount <= getPlayerMoney(player)) then local i = table.find(players, getPlayerName(player)) moneyT[i] = tostring(tonumber(moneyT[i]) + tonumber(amount)) takePlayerMoney(player, tonumber(amount)) outputChatBox ("#09ff00Wpłaciłeś na konto bankowe #ffffff$" .. amount .. "#09ff00!", player, 255, 255, 255, true) end else local size = table.size(playersT) local i = size + 1 playersT[i] = getPlayerName(player) moneyT[i] = amount takePlayerMoney(player, tonumber(amount)) outputChatBox ("#09ff00Wpłaciłeś na konto bankowe #ffffff$" .. amount .. "#09ff00!", player, 255, 255, 255, true) end end end) --Funkcja do wypłacania pieniędzy addCommandHandler("wyplac", function(player, command, amount) for k, v in ipairs(players) do if(string.match(getPlayerName(player), v)) then if amount then local i = table.find(players, getPlayerName(player)) if(tonumber(moneyT[i] >= tonumber(amount))) then moneyT[i] = tostring(tonumber(moneyT[i]) - tonumber(amount)) givePlayerMoney(player, tonumber(amount)) outputChatBox ("#09ff00Wpypłaciłeś z konta bankowego #ffffff$" .. amount .. "#09ff00!", player, 255, 255, 255, true) end end end end end) XML: <main> <players></players> <money></money> </main> Do what u want with these command handlers, make them work and give the code back, please. I have no idea how to fix this. Link to comment
Addlibs Posted January 4, 2020 Share Posted January 4, 2020 (edited) Are you trying to store player money? I'd suggest you use an XML styled more like this: <accounts> <account name="playername1" balance="1000"></account> <account name="playername2" balance="1500"></account> </accounts> And use the Lua table associatively (like a directory) instead of as an array. local moneyT = { ["playername1"] = 1000, ["playername2"] = 1500, } Generally, the way you were trying to tackle this problem is over-complicated. You're storing two related values in seperate arrays when you can use associative arrays and additionally avoid having to loop over every value searching for someone's account - if you use associative arrays, you can just index it by the player's name. --Tabele accountsT = { -- [playername] = balance } --Funkcja zczytująca zapisane tabele addEventHandler("onResourceStart", root, function() -- Open the XML local xml = xmlLoadFile("accounts.xml") if xml then local accounts = xmlNodeGetChildren(xml, "account") -- Loop through the accounts for i, node in ipairs(accounts) do local name = xmlNodeGetAttribute(node, "name") local balance = xmlNodeGetAttribute(node, "balance") -- Store `balance` in accountsT table under key `name` accountsT[name] = balance end -- Unload the XML xmlUnloadFile(xml) else -- Create XML, save and unload local xml = xmlCreateFile("accounts.xml", "accounts") xmlSaveFile(xml) xmlUnloadFile(xml) end end ) --Funkcja zapisująca tabele addEventHandler("onResourceStop", root, function() -- Create the XML local xml = xmlCreateFile("accounts.xml", "accounts") -- Loop through each account for name, balance in ipairs(accountsT) do -- Create <account> node and store account number and balance as attributes local account = xmlCreateChild(xml, "account") xmlNodeSetAttribute(account, "name", name) xmlNodeSetAttribute(account, "balance", balance) end -- Save the XML xmlSaveFile(xml) xmlUnloadFile(xml) end ) --Reszta skryptu (komendy) --Funkcja do przelewów addCommandHandler("przelew", function(player, command, pname, amount) local target = getPlayerFromName(pname) if not target then outputChatBox("ERROR_TARGET_DOES_NOT_EXIST", player, 255, 0, 0) return end -- be sure to translate this yourself -- Check if target is yourself if target == player then outputChatBox("ERROR_CANT_SENT_TO_YOURSELF", player, 255, 0, 0) return end -- be sure to translate this yourself -- Check if amount is valid amount = tonumber(amount) or 0 -- default to zero to give invalid amount error but allow math.floor to work amount = math.floor(amount) -- make sure players aren't sending non-integers if not amount or amount <= 0 then outputChatBox("ERROR_INVALID_AMOUNT", player, 255, 0, 0) return end -- be sure to translate this yourself -- If trying to send more than you have if amount > getPlayerMoney(player) then outputChatBox("ERROR_INSUFFICIENT_FUNDS", player, 255, 0, 0) return end -- be sure to translate this yourself takePlayerMoney(player, amount) givePlayerMoney(target, amount) outputChatBox("#09ff00Przelew ukończony pomyślnie!", player, 255, 255, 255, true) outputChatBox("#ffffff" .. getPlayerName(player) .. "#09ff00 przelał Ci #ffffff$" .. amount .. "#09ff00 do twojego portfela!", target, 255, 255, 255, true) end ) --Funkcja do wpłacania pieniędzy addCommandHandler("wplac", function(player, command, amount) amount = tonumber(amount) or 0 -- default to zero to given invalid amount error but allow math.floor to work amount = math.floor(amount) if not amount or amount <= 0 then outputChatBox("ERROR_INVALID_AMOUNT", player, 255, 0, 0) return end -- be sure to translate this yourself -- If trying to send more than you have if amount > getPlayerMoney(player) then outputChatBox("ERROR_INSUFFICIENT_FUNDS", player, 255, 0, 0) return end -- be sure to translate this yourself local name = getPlayerName(player) -- Create player's account if it doesn't exist yet if not accountsT[name] then accountsT[name] = 0 end takePlayerMoney(player, amount) -- take player's money accountsT[name] = accountsT[name] + amount -- and add it onto his account outputChatBox("#09ff00Wpłaciłeś na konto bankowe #ffffff$" .. amount .. "#09ff00!", player, 255, 255, 255, true) end ) --Funkcja do wypłacania pieniędzy addCommandHandler("wyplac", function(player, command, amount) amount = tonumber(amount) or 0 -- default to zero to given invalid amount error but allow math.floor to work amount = math.floor(amount) if not amount or amount <= 0 then outputChatBox("ERROR_INVALID_AMOUNT", player, 255, 0, 0) return end -- be sure to translate this yourself local name = getPlayerName(player) -- Create player's account if it doesn't exist yet if not accountsT[name] then accountsT[name] = 0 end local balance = accountsT[name] -- If trying to send more than you have if amount > balance then outputChatBox("ERROR_INSUFFICIENT_FUNDS_ON_ACCOUNT", player, 255, 0, 0) return end -- be sure to translate this yourself accountsT[name] = accountsT[name] - amount -- reduce player's account balance givePlayerMoney(player, amount) -- and give him the amount outputChatBox("#09ff00Wpypłaciłeś z konta bankowego #ffffff$" .. amount .. "#09ff00!", player, 255, 255, 255, true) end ) I haven't tested the code above, but should it not work, hopefully you'll be able to fix whatever minor bugs I've got there. Don't forget to add your translations. I've restructured it to use returns to escape out of a function to avoid indentation leading to pyramids of doom. I have also removed XML saving on player quit, since it's kinda redundant. As long as the server doesn't crash, you're safe just saving the XML when the resource is stopped. Edited January 4, 2020 by MrTasty 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