Attempt to compare number with string

I get this error every time I try to run a certain console command:



[gamemodes\GMName\gamemode\sv_shopitems.lua:8] attempt to compare number with string

I cannot find an answer to this. Here is my code:
[lua]ShopItems = {}
ShopItems[ “Run” ] = 250
ShopItems[ “Walk” ] = 250

function BuyRun( pl )
local money = pl:GetNWInt( “Money” )
local cost = ShopItems[ “Run” ]
if money >= cost then
pl:SetRunSpeed( 500 )
pl:SetMoney( money - cost )
else
end
end
concommand.Add( “bullet_shop_runspeed”, BuyRun )[/lua]

I have tried replacing:
[lua]if money >= cost then[/lua]

with:
[lua]if pl:GetNWInt( “Money” ) >= 250 then[/lua]

Thank you in advance.

I don’t personally see anything overtly wrong with it. Try putting this in after you declare the two variables:

[lua]print(type(money),type(cost))[/lua]

Oh, reread the bottom of the post, so it appears to be a pl:GetNWInt problem. Hm…

Read the wiki, it says: ““Entity:GetNWInt” can return a string in some scenarios”

What the hay? I guess the issue is you’re probably setting the money to a nil value or something.

Just type-cast both money and cost to avoid any of these issues.

It must be something with my money code… Because I set the start amount for money to 100 if the player doesn’t have a save, and it won’t set it. Going to look into it more.

[editline]07:53PM[/editline]

Ok, I still get the same error as above. I completely re-written my money system code, and no luck. Here is my money code:

[lua]local meta = FindMetaTable( “Player” )

function meta:SetMoney( amount )
local amount = self:GetMoney()
self:SetNWInt( “Money”, amount )
self:saveMoney()
end

function meta:GetMoney()
return self:GetNWInt( “Money” )
end

function meta:SaveMoney()
local current = self:GetMoney()
self:SetPData( “Cash”, current )
end

function meta:SaveMoneyTXT()
file.Write( gmod.GetGamemode().Name … “/Money/” … string.gsub( self:SteamID(), “:”, “_” ) … “.txt”, self:GetMoneyString() )
end

hook.Add( “PlayerDeath”, “GiveMoneyDeath”, function( victim, weapon, killer )
if victim:IsPlayer() and killer ~= victim then
killer:AddMoney( 15 )
end
end )

local moneyvals = {}
moneyvals[ “StartAmount” ] = 0

hook.Add( “PlayerInitialSpawn”, “GetMoneySpawn”, function( pl )
local cash = pl:GetPData( “Cash” )
if cash == nil then
pl:SetPData( “Cash”, moneyvals[ “StartAmount” ] )
pl:SetNWInt( “Money”, moneyvals[ “StartAmount” ] )
else
pl:SetNWInt( “Money”, cash )
end
end )[/lua]

Yeah, GetNWInt returns a string containing the number most of the time. All you need to do is

money = tonumber(ply:GetNWInt(“Money”))

where line 6 is in the code you posted.

Thank, it worked.

http://wiki.garrysmod.com/?title=LUA:Money_System_Part_1

Ouch :stuck_out_tongue:

Weird how I wrote the code by myself. Last time I copied that was a few months ago…

Lol its exactly the same word for word

I have a good memory :stuck_out_tongue: Anyway, my problem is fixed now.

-snip-