Jump to content

save money


Recommended Posts

Hi,

I wanted to make a script, which saves the money from a player, if he logs out or leaves, and then sets the money to 0. When he logs in again he should get the money back. With my first method it worked. I just saved the money amount in a text file. Now, I tried it with setAccountData, but there is one little Error.

My first (working) script:

  
function playerQuit() 
    local playeracc = getPlayerAccount( source ) 
    if (playeracc) and not isGuestAccount(playeracc) then 
        local playername = getPlayerName( source ) 
        local playercash = tostring(getPlayerMoney( source )) 
        local fileHandle = fileCreate( "bankaccounts/"..playername..".txt" ) 
        if fileHandle then 
            fileWrite( fileHandle, playercash ) 
            fileClose( fileHandle ) 
        end 
    end 
    setPlayerMoney( source, 0 , true ) 
end 
  
function playerLogin() 
    local playername = getPlayerName( source ) 
    local playeracc = getPlayerAccount( source ) 
    if (playeracc) and not isGuestAccount(playeracc) then 
        local playername = getPlayerName( source ) 
        local fileHandle = fileOpen( "bankaccounts/"..playername..".txt" ) 
        local filesize = fileGetSize( fileHandle ) 
        local money = fileRead( fileHandle, filesize ) 
        setPlayerMoney( source, tonumber(money), true ) 
        fileClose( fileHandle ) 
    end 
end 
addEventHandler( "onPlayerLogin", getRootElement(), playerLogin ) 
addEventHandler( "onPlayerQuit", getRootElement(), playerQuit ) 
addEventHandler( "onPlayerLogout", getRootElement(), playerQuit ) 
  

my 2nd (not working) script:

  
function playerQuit() 
    local playeracc = getPlayerAccount( source ) 
    if (playeracc) and not isGuestAccount(playeracc) then 
        local playername = getPlayerName( source ) 
        local playercash = getPlayerMoney( source ) 
        setAccountData(playeracc, "cash", playercash) 
        end 
    setPlayerMoney( source, 0 , true ) 
end 
  
function playerLogin() 
    local playername = getPlayerName( source ) 
    local playeracc = getPlayerAccount( source ) 
    if (playeracc) and not isGuestAccount(playeracc) then 
        local playername = getPlayerName( source ) 
        local money = getAccountData(playeracc, "cash") 
        setPlayerMoney( source, tonumber(money), true ) 
    end 
end 
addEventHandler( "onPlayerLogin", getRootElement(), playerLogin ) 
addEventHandler( "onPlayerQuit", getRootElement(), playerQuit ) 
addEventHandler( "onPlayerLogout", getRootElement(), playerQuit ) 
  

The error is in line 18. It says, it setPlayerMoney, expected a number in argument 2, but got nil. But I saved it as a number, didn't I? And also I wrote tonumber.

would appreciate help.

Mathias Lui

Link to comment
      
    function playerQuit() 
        local playeracc = getPlayerAccount( source ) 
        if (playeracc) and not isGuestAccount(playeracc) then 
            --local playername = getPlayerName( source ) 
            local playercash = tonumber(getPlayerMoney( source )) 
            setAccountData(playeracc, "cash", playercash) 
        end 
        setPlayerMoney(source,0) 
    end 
      
    function playerLogin() 
        local playername = getPlayerName( source ) 
        local playeracc = getPlayerAccount( source ) 
        if (playeracc) and not isGuestAccount(playeracc) then 
            local money = getAccountData(playeracc, "cash") 
            if money then 
            setPlayerMoney( source, money ) 
            end 
        end 
    end 
    addEventHandler( "onPlayerLogin", getRootElement(), playerLogin ) 
    addEventHandler( "onPlayerQuit", getRootElement(), playerQuit ) 
    addEventHandler( "onPlayerLogout", getRootElement(), playerQuit ) 
      

Link to comment
function playerQuit() 
    local playeracc = getPlayerAccount( source ) 
    if (playeracc) and not isGuestAccount(playeracc) then 
        local playercash = tonumber(getPlayerMoney( source )) 
        setAccountData(playeracc, "cash", playercash) 
    end 
end 
addEventHandler( "onPlayerQuit", getRootElement(), playerQuit ) 
addEventHandler( "onPlayerLogout", getRootElement(), playerQuit ) 
  
function playerLogin(_,account) 
    local money = getAccountData(account, "cash") 
    if money then 
        setPlayerMoney(source, tonumber(money)) 
    end 
end 
addEventHandler( "onPlayerLogin", getRootElement(), playerLogin ) 
  

Link to comment
local playercash = tonumber(getPlayerMoney( source )) 

I wonder why you set the getPlayerMoney function's return value to number and it returns already an integer value by default.

it seems like that, converting a number to a number which makes no sense.

tonumber ( 104350 ) 

Link to comment
      
    function playerQuit() 
        local playeracc = getPlayerAccount( source ) 
        if (playeracc) and not isGuestAccount(playeracc) then 
            --local playername = getPlayerName( source ) 
            local playercash = tonumber(getPlayerMoney( source )) 
            setAccountData(playeracc, "cash", playercash) 
        end 
        setPlayerMoney(source,0) 
    end 
      
    function playerLogin() 
        local playername = getPlayerName( source ) 
        local playeracc = getPlayerAccount( source ) 
        if (playeracc) and not isGuestAccount(playeracc) then 
            local money = getAccountData(playeracc, "cash") 
            if money then 
            setPlayerMoney( source, money ) 
            end 
        end 
    end 
    addEventHandler( "onPlayerLogin", getRootElement(), playerLogin ) 
    addEventHandler( "onPlayerQuit", getRootElement(), playerQuit ) 
    addEventHandler( "onPlayerLogout", getRootElement(), playerQuit ) 
      

He'll surely use the playername for something, so no point of deadlining it in the 1st function... I guess he'll want to make a debug output or something,idk.

Basically the code from the poster is good, it hasn't got any error.

I think it returns nil because you didn't try it after logging in... But your code is good.

Link to comment

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