bartje01 Posted March 7, 2011 Posted March 7, 2011 Hey guys. I tried to make a /setcash cmd myself. function setCash ( sourcePlayer,givedname,amount) local gived = getPlayerFromNick ( givedname ) if not isGuestAccount ( getPlayerAccount ( gived ) ) then local getPlayerCash = getAccountData ( account, "data.cash" ) setAccountData ( account, "data.cash", tonumber(getPlayerCash) + 100 ) outputChatBox ( "Your cash has been setted.", gived ) outputChatBox ( "You've setted his cash.", sourcePlayer ) end end addCommandHandler ( "setcash", setCash ) I was already glad that I had no errors but it doesn't work at all. Just no reaction on my cmd. What is wrong? Thankyou
Castillo Posted March 7, 2011 Posted March 7, 2011 You copied parts of my code and failed function setCash ( sourcePlayer,givedname,amount) local gived = getPlayerFromName ( givedname ) if not isGuestAccount ( getPlayerAccount ( gived ) ) then account = getPlayerAccount(gived) local getPlayerCash = getAccountData ( account, "data.cash" ) setAccountData ( account, "data.cash", tonumber(getPlayerCash) + tonumber(amount) or 100 ) outputChatBox ( "Your cash has been setted.", gived ) outputChatBox ( "You've setted his cash.", sourcePlayer ) end end addCommandHandler ( "setcash", setCash ) Should work.
bartje01 Posted March 7, 2011 Author Posted March 7, 2011 I indeed took the lines from you and started to play around with them. That's my way of learning to script. The code didn't had effect by the way :*(
bartje01 Posted March 7, 2011 Author Posted March 7, 2011 What I have: function myCash ( thePlayer ) if not isGuestAccount ( getPlayerAccount ( thePlayer ) ) then account = getPlayerAccount(thePlayer) local cash = getAccountData ( account, "data.cash" ) outputChatBox ( "Your cash is: " .. tostring ( cash ) .. " $ ", thePlayer ) end end addCommandHandler ( "cash", myCash ) function setCash ( sourcePlayer,givedname,amount) local gived = getPlayerFromName ( givedname ) if not isGuestAccount ( getPlayerAccount ( gived ) ) then account = getPlayerAccount(gived) local getPlayerCash = getAccountData ( account, "data.cash" ) setAccountData ( account, "data.cash", tonumber(getPlayerCash) + tonumber(amount) or 100 ) outputChatBox ( "Your cash has been setted.", gived ) outputChatBox ( "You've setted his cash.", sourcePlayer ) end end addCommandHandler ( "setcash", setCash ) I also gave you another funtion It doesn't have effect ingame. But I have warning codes [2011-03-07 16:08:13] WARNING: testmode\server.lua:70: Bad argument @ 'getPlayerAccount' [2011-03-07 16:08:13] WARNING: testmode\server.lua:70: Bad argument @ 'isGuestAccount' [2011-03-07 16:08:13] WARNING: testmode\server.lua:71: Bad argument @ 'getPlayerAccount' [2011-03-07 16:08:13] WARNING: testmode\server.lua:72: Bad argument @ 'getAccountData' [2011-03-07 16:08:13] ERROR: testmode\server.lua:73: attempt to perform arithmetic on a nil value
Dre-+- Posted March 7, 2011 Posted March 7, 2011 Use this: function myCash ( thePlayer ) if not isGuestAccount ( getPlayerAccount ( thePlayer ) ) then account = getPlayerAccount(thePlayer) local cash = getAccountData ( account, "data.cash" ) outputChatBox ( "Your cash is: " .. tostring ( cash ) .. " $ ", thePlayer ) end end addCommandHandler ( "cash", myCash ) function setCash ( sourcePlayer,cmd,givedname,amount) local gived = getPlayerFromName ( givedname ) if not isGuestAccount ( getPlayerAccount ( gived ) ) then account = getPlayerAccount(gived) local getPlayerCash = getAccountData ( account, "data.cash" ) setAccountData ( account, "data.cash", tonumber(getPlayerCash) + tonumber(amount) or 100 ) outputChatBox ( "Your cash has been setted.", gived ) outputChatBox ( "You've setted his cash.", sourcePlayer ) end end addCommandHandler ( "setcash", setCash )
bartje01 Posted March 7, 2011 Author Posted March 7, 2011 Still same warnings and errors [2011-03-07 16:08:13] WARNING: testmode\server.lua:70: Bad argument @ 'getPlayerAccount' [2011-03-07 16:08:13] WARNING: testmode\server.lua:70: Bad argument @ 'isGuestAccount' [2011-03-07 16:08:13] WARNING: testmode\server.lua:71: Bad argument @ 'getPlayerAccount' [2011-03-07 16:08:13] WARNING: testmode\server.lua:72: Bad argument @ 'getAccountData' [2011-03-07 16:08:13] ERROR: testmode\server.lua:73: attempt to perform arithmetic on a nil value
Moderators Citizen Posted March 7, 2011 Moderators Posted March 7, 2011 Why you try to getPlayerAccount with the PlayerName ?? You have to specify the playerElement, not his name so replace this: if not isGuestAccount ( getPlayerAccount ( gived ) ) then by this: if not isGuestAccount ( getPlayerAccount ( sourcePlayer) ) then And this: account = getPlayerAccount(gived) by this: account = getPlayerAccount(sourcePlayer) and it should work now The wiki is your friend ! ( I never used this function and I just saw the wiki and I found this: thePlayer: The player element you want to get the account of. )
Castillo Posted March 7, 2011 Posted March 7, 2011 (edited) Acording to what i understand, he wants to do something like /setcash playername amount so he must use getPlayerFromName function setCash ( sourcePlayer,cmd,who,amount) local gived = getPlayerFromName ( who ) if not gived then gived = sourcePlayer end if not isGuestAccount ( getPlayerAccount ( gived ) ) then account = getPlayerAccount(gived) local getPlayerCash = getAccountData ( account, "data.cash" ) setAccountData ( account, "data.cash", tonumber(getPlayerCash) + tonumber(amount) or 100 ) outputChatBox ( "Your cash has been setted.", gived ) outputChatBox ( "You've setted his cash.", sourcePlayer ) end end addCommandHandler ( "setcash", setCash ) Try it. Edited March 7, 2011 by Guest
Moderators Citizen Posted March 7, 2011 Moderators Posted March 7, 2011 Oh ok I understand now And I have a question at this line: if not gived then gived = sourcePlayer The end is not compulsory ? if not gived then gived = sourcePlayer end
bartje01 Posted March 7, 2011 Author Posted March 7, 2011 I use solidsnake's code now. It only works when I type a name that doesn't exists. Then it sets my money But when I try to do set someone his cash that's online it doesn't have effect again ERROR: testmode\server.lua:84: attempt to perform arithmetic on a nil value
Castillo Posted March 7, 2011 Posted March 7, 2011 Oh ok I understand now And I have a question at this line: if not gived then gived = sourcePlayer The end is not compulsory ? if not gived then gived = sourcePlayer end Oh, my bad i forgot about that
Moderators Citizen Posted March 7, 2011 Moderators Posted March 7, 2011 I made some modifications test it please: function setCash ( sourcePlayer,cmd,who,amount) if ( not who and not amount ) then outputChatBox("*Use /setcash [Name] [amount]", sourcePlayer, 180, 0, 0 ) end local gived = getPlayerFromName ( who ) if not gived then gived = sourcePlayer end account = getPlayerAccount(gived) if not isGuestAccount ( account ) then local playerCash = getAccountData ( account, "data.cash" ) local success = setAccountData ( account, "data.cash", tonumber(playerCash)+tonumber(amount) ) if success then outputChatBox ( "Your cash has been setted.", gived ) outputChatBox ( "You've setted his cash.", sourcePlayer ) else outputChatBox("ERROR: His cash his not setted", sourcePlayer, 180, 0, 0 ) end end end addCommandHandler ( "setcash", setCash ) It works perfectly in my brain
bartje01 Posted March 7, 2011 Author Posted March 7, 2011 Thanks citizen and other guys. it works now. BUt how can I make that if the player isn't online it says: player not connected
Timic Posted March 7, 2011 Posted March 7, 2011 function setCash ( sourcePlayer,cmd,who,amount) if ( not who and not amount ) then outputChatBox("*Use /setcash [Name] [amount]", sourcePlayer, 180, 0, 0 ) end local gived = getPlayerFromName ( who ) if not gived then gived = sourcePlayer end account = getPlayerAccount(gived) if not isGuestAccount ( account ) then local playerCash = getAccountData ( account, "data.cash" ) local success = setAccountData ( account, "data.cash", tonumber(playerCash)+tonumber(amount) ) if success then outputChatBox ( "Your cash has been setted.", gived ) outputChatBox ( "You've setted his cash.", sourcePlayer ) else outputChatBox("ERROR: Player is not connected", sourcePlayer, 180, 0, 0 ) end end end addCommandHandler ( "setcash", setCash ) Well, I didn't test it yet
bartje01 Posted March 7, 2011 Author Posted March 7, 2011 Still when I type /setcash ballablbl 124 it sets my money
Castillo Posted March 7, 2011 Posted March 7, 2011 function setCash ( sourcePlayer,cmd,who,amount) if ( not who and not amount ) then outputChatBox("*Use /setcash [Name] [amount]", sourcePlayer, 180, 0, 0 ) end local gived = getPlayerFromName ( who ) account = getPlayerAccount(gived) if not isGuestAccount ( account ) then local playerCash = getAccountData ( account, "data.cash" ) local success = setAccountData ( account, "data.cash", tonumber(playerCash)+tonumber(amount) ) if success then outputChatBox ( "Your cash has been setted.", gived ) outputChatBox ( "You've setted his cash.", sourcePlayer ) else outputChatBox("ERROR: Player is not connected", sourcePlayer, 180, 0, 0 ) end end end addCommandHandler ( "setcash", setCash )
Moderators Citizen Posted March 7, 2011 Moderators Posted March 7, 2011 Thanks citizen and other guys.it works now. BUt how can I make that if the player isn't online it says: player not connected Like this: function setCash ( sourcePlayer,cmd,who,amount) if ( not who and not amount ) then outputChatBox("*Use /setcash [Name] [amount]", sourcePlayer, 180, 0, 0 ) end local gived = getPlayerFromName ( who ) if not gived then outputChatBox("The player "..who.." is not connected", sourcePlayer, 180, 0, 0 ) end account = getPlayerAccount(gived) if not isGuestAccount ( account ) then local playerCash = getAccountData ( account, "data.cash" ) local success = setAccountData ( account, "data.cash", tonumber(playerCash)+tonumber(amount) ) if success then outputChatBox ( "Your cash has been setted.", gived ) outputChatBox ( "You've setted his cash.", sourcePlayer ) else outputChatBox("ERROR: His cash his not setted", sourcePlayer, 180, 0, 0 ) end end end addCommandHandler ( "setcash", setCash ) No problem bartje01
bartje01 Posted March 7, 2011 Author Posted March 7, 2011 Yay it works. But 3 warnings left when I type a wrong name. But it says player not connected though. SO it works it would be nice if we can fix these warnings [2011-03-07 21:26:52] WARNING: testmode\server.lua:82: Bad argument @ 'getPlayerAccount' [2011-03-07 21:26:52] WARNING: testmode\server.lua:83: Bad argument @ 'isGuestAccount' [2011-03-07 21:26:52] WARNING: testmode\server.lua:84: Bad argument @ 'getAccountData' [2011-03-07 21:26:52] ERROR: testmode\server.lua:85: attempt to perform arithmetic on a nil value That's weird Thankyou very much btw
12p Posted March 7, 2011 Posted March 7, 2011 function setCash ( sourcePlayer,cmd,who,amount) if ( not who and not amount ) then outputChatBox("*Use /setcash [Name] [amount]", sourcePlayer, 180, 0, 0 ) end local gived = getPlayerFromName ( who ) if not gived then outputChatBox("The player "..who.." is not connected", sourcePlayer, 180, 0, 0 ) return end --If you want to stop the function, use "return". account = getPlayerAccount(gived) if not isGuestAccount ( account ) then local playerCash = getAccountData ( account, "data.cash" ) local success = setAccountData ( account, "data.cash", tonumber(playerCash)+tonumber(amount) ) if success then outputChatBox ( "Your cash has been setted.", gived ) outputChatBox ( "You've setted his cash.", sourcePlayer ) else outputChatBox("ERROR: His cash his not setted", sourcePlayer, 180, 0, 0 ) end end end addCommandHandler ( "setcash", setCash )
bartje01 Posted March 7, 2011 Author Posted March 7, 2011 Nice. 2 warnings removed thanks 2 left [2011-03-07 22:09:54] WARNING: testmode\server.lua:80: Bad argument @ 'getPlayerFromName' [2011-03-07 22:09:54] ERROR: testmode\server.lua:81: attempt to concatenate local 'who' (a nil value)
proracer Posted March 7, 2011 Posted March 7, 2011 I have tried your code and it works in my local server... Make sure you type: /setcash Example: /setcash proracer 100
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