Jump to content

Setcash command


bartje01

Recommended Posts

Posted

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

Posted

You copied parts of my code and failed :P

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.

Posted

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 :*(

Posted

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 
  

Posted

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 ) 
  

Posted

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
Posted

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 ! :mrgreen:

( 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. )

Posted (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 by Guest
  • Moderators
Posted

Oh ok I understand now :lol:

And I have a question at this line:

if not gived then gived = sourcePlayer 

The end is not compulsory ? :arrowdown:

if not gived then gived = sourcePlayer end 

Posted

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

Posted
Oh ok I understand now :lol:

And I have a question at this line:

if not gived then gived = sourcePlayer 

The end is not compulsory ? :arrowdown:

if not gived then gived = sourcePlayer end 

Oh, my bad i forgot about that :P

  • Moderators
Posted

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 :mrgreen:

Posted
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 :mrgreen:

Posted
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
Posted
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

Posted

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

Posted
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 ) 

Posted

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)

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...