Hello everyone, I'm trying to create a little script that when a player presses a button on a menu it removes $20,000 from them and saves "Jeep" to a text file.
Here is the error I'm getting
learning\gamemode\money_system.lua:35] attempt to compare number with nil
Here's line 34-40 of my money_system code.
[LUA]
function meta:HasMoney( amount )
if tonumber(self:GetPData( MoneyIdentity )) >= amount then -- Line 35
return true;
else
return false;
end;
end;
[/LUA]
Here's the code that actually removes the $20,000 and saves "Jeep" to a text file.
[LUA]
local meta = FindMetaTable("Player")
function meta:Jeep( )
local contents = file.Read("Cars/" .. self:FormatSteamID() .. ".txt" )
if self:HasMoney( 20000 ) then
if file.Exists("Cars/" .. self:FormatSteamID() .. ".txt" ) then
if contents == "Jeep" then
self:ChatPrint("You already have this car!")
else
file.Append("Cars/" ..self:FormatSteamID() .. ".txt", "\nJeep" ) -- If the file does exist and the player doesn't have "Jeep" then edit the file and add Jeep
self:AddMoney( - 20000 )
end
else file.Write( "Cars/" .. self:FormatSteamID( ) .. ".txt", "Jeep" ) -- If the file doesn't exsist then we write it
end
else self:ChatPrint("Sorry you don't have enough cash!")
end
end
[/LUA]
I'm stumped right now, any help would be appreciated.
Change line 35 to:
[lua]
if ( self:GetPData(MoneyIdentity, 0) >= amount) then
[/lua]
[QUOTE=Chessnut;36011547]Change line 35 to:
[lua]
if ( self:GetPData(MoneyIdentity, 0) >= amount) then
[/lua][/QUOTE]
I changed line 35 to that and now I'm getting a new error. I changed the code back to what it was before and I'm still getting it.
Timer Error: [@learning\gamemode\money_system.lua:44] bad argument #2 to 'Write' (string expected, got nil)
[LUA]
function meta:SaveMoney( )
file.Write( "player_money/" .. self:FormatSteamID( ) .. ".txt", self:GetMoney( ) ); -- Line 44
end;
-- Saves money every 60 seconds
timer.Create( "MoneySave", 60, 0, function( )
for k, v in pairs( player.GetAll( ) ) do
v:SaveMoney( );
end;
end );
[/LUA]
GetMoney() isn't returning anything.
[QUOTE=Chessnut;36011963]GetMoney() isn't returning anything.[/QUOTE]
Oh alright. I fixed the error but now when I press the button it just does the ChatPrint("Sorry you don't have enough cash!") even when I have $300,000.
debug your code bro, I've had plenty of problems like this.
Do stuff like
[lua]
Msg(ply:Nick().."'s money should be 300,000)
local money = ply:GetMoney()
Msg(ply:Nick().."'s money appears to be: "..money)
[/lua]
I know it's long, but it's one way to debug your code....
I Fail At Lua's money should be 300,000 I Fail At Lua's money appears to be: 304450. And when I go check the file that the money is saved in it says that amount to. So I have no clue what is wrong. Anyone else have any ideas?
Try, the following. Im assuming that "amount" is being passed with a decimal.
[lua]
function meta:HasMoney( amount )
amount = tonumber(amount)
if tonumber(self:GetPData( MoneyIdentity )) >= amount then -- Line 35
return true;
else
return false;
end;
end;
[/lua]
Hmmmm, now I seem to be getting this error:
[learning\gamemode\money_system.lua:36] attempt to compare number with nil
Lets do a quick debug
[lua]
function meta:HasMoney( amount )
print(amount)
print(self:GetPData( MoneyIdentity ))
if tonumber(self:GetPData( MoneyIdentity )) >= amount then -- Line 35
return true;
else
return false;
end;
end;
[/lua]
20000
nil
[learning\gamemode\money_system.lua:37] attempt to compare number with nil
MoneyIdentity is nil. Is it localized in another file when it should be globalized perhaps? Show me where MoneyIdentity is being set/what it is.
At the top of the file I have:
[LUA]
local meta = FindMetaTable( "Player" )
local MoneyIdentity = "PlayerMoney"
local SalaryIdentity = "PlayerSalary"
[/LUA]
Should it be global instead of local?
[QUOTE=I Fail At Lua;36020273]At the top of the file I have:
[LUA]
local meta = FindMetaTable( "Player" )
local MoneyIdentity = "PlayerMoney"
local SalaryIdentity = "PlayerSalary"
[/LUA]
Should it be global instead of local?[/QUOTE]
local's can only operate in same file, function, loop, etc.
When I make them global it still seems to be giving me the same error, and its still saying MoneyIdentity is nil.
[lua]
function meta:HasMoney( amount )
local cash = self:GetPData("PlayerMoney")
if type(cash) != "number" then print("Players cash isnt a number") return end
if cash >= amount then
return true;
else
return false;
end;
end;
[/lua]
Sorry you don't have enough cash!
Players cash isnt a number
Shoot, I have to go for a little bit. I'll be back in a bit hopefully.
Sorry, you need to Log In to post a reply to this thread.