SinaAmp Posted February 1, 2022 Share Posted February 1, 2022 Hello guys I want to give and take money from a specific player and update database data for that player using command i wrote my script but that give money only to me here is my script: function moneyCmd(player, commandName, AccName, amount) if(getElementType(player) ~= "player") then return end pacc = getPlayerAccount (player) AccName = getAccountName (pacc) if isObjectInACLGroup ("user."..AccName, aclGetGroup ( "Moderator" ) ) then -- the shared logic if commandName == "givemoney" then amount = tonumber(amount) if tostring(AccName) and amount then local PMoney = givePlayerMoney(player, amount) dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",PMoney,AccName) else outputChatBox("[usage] /givemoney [playername] [amount]", player) end else if commandName == "takemoney" then amount = tonumber(amount) if tostring(AccName) and amount then takePlayerMoney(player, amount) dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",PMoney,AccName) else outputChatBox("[usage] /takemoney [playername] [amount]", player) end else outputChatBox("kolan ridi", player) end end else outputChatBox("You aren't able to use this command", player) end end addCommandHandler("givemoney", moneyCmd); addCommandHandler("takemoney", moneyCmd); Link to comment
SinaAmp Posted February 1, 2022 Author Share Posted February 1, 2022 edited a little bit: Spoiler function moneyCmd(player, commandName, AccName, amount) if(getElementType(player) ~= "player") then return end pacc = getPlayerAccount (player) AccName = getAccountName (pacc) if isObjectInACLGroup ("user."..AccName, aclGetGroup ( "Moderator" ) ) then -- the shared logic if commandName == "givemoney" then amount = tonumber(amount) if tostring(AccName) and amount then local playermoney = getPlayerMoney(player) local sumpmoney = tonumber(playermoney) + tonumber(amount) givePlayerMoney(player, amount) dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",sumpmoney,AccName) else outputChatBox("[usage] /givemoney [playername] [amount]", player) end else if commandName == "takemoney" then amount = tonumber(amount) if tostring(AccName) and amount then local playermoney = getPlayerMoney(player) local subpmoney = tonumber(playermoney) - tonumber(amount) takePlayerMoney(player, amount) dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",subpmoney,AccName) else outputChatBox("[usage] /takemoney [playername] [amount]", player) end else outputChatBox("kolan ridi", player) end end else outputChatBox("You aren't able to use this command", player) end end addCommandHandler("givemoney", moneyCmd); addCommandHandler("takemoney", moneyCmd); Link to comment
Yeleha Posted February 1, 2022 Share Posted February 1, 2022 (edited) 1 hour ago, SinaAmp said: edited a little bit: Hide contents function moneyCmd(player, commandName, AccName, amount) if(getElementType(player) ~= "player") then return end pacc = getPlayerAccount (player) AccName = getAccountName (pacc) if isObjectInACLGroup ("user."..AccName, aclGetGroup ( "Moderator" ) ) then -- the shared logic if commandName == "givemoney" then amount = tonumber(amount) if tostring(AccName) and amount then local playermoney = getPlayerMoney(player) local sumpmoney = tonumber(playermoney) + tonumber(amount) givePlayerMoney(player, amount) dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",sumpmoney,AccName) else outputChatBox("[usage] /givemoney [playername] [amount]", player) end else if commandName == "takemoney" then amount = tonumber(amount) if tostring(AccName) and amount then local playermoney = getPlayerMoney(player) local subpmoney = tonumber(playermoney) - tonumber(amount) takePlayerMoney(player, amount) dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",subpmoney,AccName) else outputChatBox("[usage] /takemoney [playername] [amount]", player) end else outputChatBox("kolan ridi", player) end end else outputChatBox("You aren't able to use this command", player) end end addCommandHandler("givemoney", moneyCmd); addCommandHandler("takemoney", moneyCmd); local playermoney = getPlayerMoney(player) Player in this case is the source that is executing it, so you. If you want to set it to a player with an account that is currently logged in you should do that with a loop. for k,v in pairs(getElementsByType("player")) do if getAccountName(v) == AccName then -- This can cause problems if more players are on the same account, giving all of them money givePlayerMoney(v, amount) end end Edited February 1, 2022 by Derbo 1 Link to comment
SinaAmp Posted February 1, 2022 Author Share Posted February 1, 2022 thank you @Derbo but im searching for the way to change offline players money too in database Link to comment
Yeleha Posted February 1, 2022 Share Posted February 1, 2022 4 minutes ago, SinaAmp said: thank you @Derbo but im searching for the way to change offline players money too in database Use Amount instead of sumpmoney. Link to comment
βurak Posted February 1, 2022 Share Posted February 1, 2022 (edited) I'm not sure but can you test this code the problem might be that you assign your own account to your AccName variable in the first place function moneyCmd(player, commandName, AccName, amount) --if(getElementType(player) ~= "player") then return end -- not need this --pacc = getPlayerAccount (player) -- You assign your own account to the accName variable --AccName = getAccountName (pacc) -- try remove this local targetAccount = getAccount(AccName) -- get account from AccName if not (targetAccount) then return outputChatBox("There is no such account in the database.", player) end --Check if there is an account named accName, if not, don't continue if isObjectInACLGroup ("user."..AccName, aclGetGroup ( "Moderator" ) ) then -- the shared logic if commandName == "givemoney" then amount = tonumber(amount) if tostring(AccName) and amount then local PMoney = givePlayerMoney(player, amount) dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",PMoney,AccName) else outputChatBox("[usage] /givemoney [playername] [amount]", player) end else if commandName == "takemoney" then amount = tonumber(amount) if tostring(AccName) and amount then takePlayerMoney(player, amount) dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",PMoney,AccName) else outputChatBox("[usage] /takemoney [playername] [amount]", player) end else outputChatBox("kolan ridi", player) end end else outputChatBox("You aren't able to use this command", player) end end addCommandHandler("givemoney", moneyCmd); addCommandHandler("takemoney", moneyCmd); Edited February 1, 2022 by Burak5312 1 Link to comment
SinaAmp Posted February 1, 2022 Author Share Posted February 1, 2022 (edited) do you mean clculate that in dbexec parameter? @Derbo thank you bro @Burak5312 i fixed that before: Spoiler function moneyCmd(player, commandName, InAccName, amount) if(getElementType(player) ~= "player") then return end pacc = getPlayerAccount (player) AccName = getAccountName (pacc) if isObjectInACLGroup ("user."..AccName, aclGetGroup ( "Moderator" ) ) then -- if player is in acl group if commandName == "givemoney" then local amount = tonumber(amount) if amount then for k,v in pairs(getElementsByType("player")) do if getAccountName(pacc) == InAccName then local playermoney = getPlayerMoney(player) local sumpmoney = tonumber(playermoney) + tonumber(amount) dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",sumpmoney,AccName) givePlayerMoney(v, amount) end end else outputChatBox("[usage] /givemoney [playername] [amount]", player) end else if commandName == "takemoney" then local amount = tonumber(amount) if amount then for k,v in pairs(getElementsByType("player")) do if getAccountName(pacc) == InAccName then local playermoney = getPlayerMoney(player) local subpmoney = tonumber(amount) - tonumber(playermoney) dbExec(db,"UPDATE stats SET Money=? WHERE Account=?",subpmoney,AccName) takePlayerMoney(v, amount) end end else outputChatBox("[usage] /takemoney [playername] [amount]", player) end else outputChatBox("kolan ridi", player) end end else outputChatBox("You aren't able to use this command", player) end end addCommandHandler("givemoney", moneyCmd); addCommandHandler("takemoney", moneyCmd); but i want to change money of the offline players and now i working on that Edited February 1, 2022 by SinaAmp 1 Link to comment
βurak Posted February 1, 2022 Share Posted February 1, 2022 givePlayerMoney(v, amount) local playermoney = getPlayerMoney(player) takePlayerMoney(v, amount) remove codes like this because this only applies to players that exist on the server if you want to do it from the database just sql query is enough Link to comment
SinaAmp Posted February 1, 2022 Author Share Posted February 1, 2022 @Burak5312can you give me a final edit? cuse i added your script and amount not saved in database Link to comment
βurak Posted February 1, 2022 Share Posted February 1, 2022 (edited) Can you try this, sorry for the messy codes, the forum is breaking it function moneyCmd(player, commandName, AccName, amount) --if(getElementType(player) ~= "player") then return end -- not need this --pacc = getPlayerAccount (player) -- You assign your own account to the accName variable --AccName = getAccountName (pacc) -- try remove this local targetAccount = getAccount(AccName) -- get account from AccName if not (targetAccount) then return outputChatBox("There is no such account in the database.", player) end --Check if there is an account named accName, if not, don't continue if isObjectInACLGroup ("user."..AccName, aclGetGroup ( "Moderator" ) ) then -- the shared logic local targetMoney local MoneyData = dbPoll(dbQuery(db,"SELECT Money FROM stats WHERE Account=? LIMIT 1",AccName), -1) if(MoneyData) then if(#MoneyData > 0) then for _,row in ipairs(MoneyData) do targetMoney = row["Money"] -- get player money amount from database break end end end if commandName == "givemoney" then amount = tonumber(amount) if tostring(AccName) and amount then targetMoney = targetMoney + amount -- addition with amount dbExec(db,"UPDATE stats SET Money=? WHERE Account=?", targetMoney,AccName) else outputChatBox("[usage] /givemoney [playername] [amount]", player) end else if commandName == "takemoney" then amount = tonumber(amount) if tostring(AccName) and amount then targetMoney = targetMoney - amount -- subtraction with amount dbExec(db,"UPDATE stats SET Money=? WHERE Account=?", targetMoney,AccName) else outputChatBox("[usage] /takemoney [playername] [amount]", player) end else outputChatBox("kolan ridi", player) end end else outputChatBox("You aren't able to use this command", player) end end addCommandHandler("givemoney", moneyCmd); addCommandHandler("takemoney", moneyCmd); Edited February 1, 2022 by Burak5312 1 Link to comment
SinaAmp Posted February 1, 2022 Author Share Posted February 1, 2022 @Burak5312 thank you bro but in this line of code i think we need to subtract amount from targetmoney. isn't it? targetMoney = targetMoney - amount Link to comment
βurak Posted February 1, 2022 Share Posted February 1, 2022 Just now, SinaAmp said: @Burak5312 thank you bro but in this line of code i think we need to subtract amount from targetmoney. isn't it? targetMoney = targetMoney - amount yes, this is what you need to do for the takemoney command, it will subtract the amount from the player's current money Link to comment
SinaAmp Posted February 1, 2022 Author Share Posted February 1, 2022 i know but that store a negative number in database that might be like this in takemoney cmd condition: targetMoney = amount - targetMoney Link to comment
βurak Posted February 1, 2022 Share Posted February 1, 2022 (edited) Does this player work even though he has money? this can happen if the amount you entered is more than the player's money maybe you can add control like this if(targetMoney - amount < 0) then return end Edited February 1, 2022 by Burak5312 Link to comment
SinaAmp Posted February 1, 2022 Author Share Posted February 1, 2022 Thank you bro I try this later. Link to comment
βurak Posted February 1, 2022 Share Posted February 1, 2022 1 minute ago, SinaAmp said: Thank you bro I try this later. okey your welcome Link to comment
SinaAmp Posted February 1, 2022 Author Share Posted February 1, 2022 hello bro @Burak5312 again me i want to save player health after player quit but nothing change in database function phealth (_,account) local AccName = getAccountName(account) local playerhealth = getElementHealth(source) dbExec(db,"UPDATE stats SET Health=? WHERE Account=?",playerhealth, AccName) end addEventHandler("onplayerQuit", root, phealth) Link to comment
βurak Posted February 1, 2022 Share Posted February 1, 2022 function phealth () -- onPlayerQuit event does not keep accounts local playerAccount = getPlayerAccount(source) if (playerAccount) then if(isGuestAccount(playerAccount)) then return end -- if guest account don't continue end local AccName = getAccountName(playerAccount) local playerhealth = getElementHealth(source) dbExec(db,"UPDATE stats SET Health=? WHERE Account=?",playerhealth, AccName) end addEventHandler("onPlayerQuit", root, phealth) --there is a typo here 1 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