Confusing gamemode problem (Money)

Hello, This isn’t really easy to explain, so i’ll do my best.

There is something wrong with the following function.

[lua]
function GM:PlayerInitialSpawn( ply )

ply:SetNWInt( “Stamina”, 100 )

    local cash = ply:GetPData("money")
   
    --if cash == nil then
    --        ply:SetPData("money", MONEY_STARTAMOUNT)
    --        ply:SetMoney( MONEY_STARTAMOUNT )
    --else
    -- ply:SetMoney( cash )
    --end

end
[/lua]

The lines that are commented out is where the problem seems to be

It sets a players money properly upon first spawn, but what I don’t understand is, when ever I go to buy a weapon from the weapon menu in my gamemode, this error pops up (This is for only 1 of the weapons):


gamemodes\bonkdm\gamemode\init.lua:183: attempt to compare string with number

And it does not let me buy weapons from the menu until I get at least 1 kill.

If I remove “if cash == nil then” and replace it with “if cash == 0 then”, people can access the weapons, but the money doesn’t load.

I’ve tried using files but I have no idea how to use the file read/load crap. It doesn’t like me.

Anyways, any ideas?

Try [lua]if tonumber(cash) then[/lua]

As a side note, I just was wondering about your BonkDM server, and since it was locked, I visited your site. Coincidences.

Oh wow, now I saw you randomly on a Gmod Racer server that I decided to join.

Also try
[lua]ply:SetMoney( tonumber(cash) )[/lua]

I don’t think PData returns as a number.

I got it working.

[lua]
function GM:PlayerInitialSpawn( ply )

ply:SetNWInt( “Stamina”, 100 )

    local cash = ply:GetPData("money")
   
	if tonumber(cash) == 0 then  
            ply:SetPData("money", MONEY_STARTAMOUNT)
            ply:SetMoney( MONEY_STARTAMOUNT )
    else
    ply:SetMoney(tonumber(cash))
    end

end
[/lua]

Thanks for the help, guys!

What would happen if a player spent all their money - and had $0 left, and then rejoined? They would get the STARTAMOUNT instead of staying on zero. Check if cash is nil instead of zero.

[lua]if tonumber(cash) == nil then[/lua]

I lol’d

cash = tonumber( cash ) or MONEY_STARTAMOUNT

PData should return a string. If the return value is non-numeric, we can assume they don’t have their money set ( “”, nil, etc ) or it was somehow tampered with/overwrote by something else.

It’s both of our gamemode, we did like 50/50. I’m not going to call it “Mine and Co bullet’s gamemode” every time. -.-