'for' limit must be a number

[lua]
function meta:LoadPlayerData( )
// Similar to the file.append that is supplied by alzui sleet’s gg module.
local ID = string.gsub( self:SteamID( ), “:”, “_” )
if ( !file.Exists( “Players/” … ID … “/data.txt” ) ) then self:SetBank( 100 ) self:ChatPrint( “Welcome [NEW]” … self:Nick() … " your Garry Points[GP] has been set to 100!" ) self:SavePlayerData( ) end
if ( file.Exists( “Players/” … ID … “/data.txt” ) ) then
local Data = util.KeyValuesToTable( file.Read( “Players/” … ID … “/data.txt” ) )
self:SetBank( tonumber( Data[ “bank” ] ) )
end
if ( file.Exists( “Players/” … ID … “/inventory.txt” ) ) then
local DataInv = util.KeyValuesToTable( file.Read( “Players/” … ID … “/inventory.txt” ) )
for i = 1, tonumber( DataInv[ “amount” ] ) do
self:ItemAdd( tostring( DataInv[ “class” ] ) )
end
end
self:ChatPrint( “Garry Points[GP] is currently in beta!” )
self:ChatPrint( “Welcome " … self:Nick() … " your Garry Point(s)[GP] data was loaded!” )
self:ChatPrint( self:Nick() … " your current balance is " … self:Bank( ) … “[GP].” )
end
[/lua]

[lua]for i = 1, tonumber( DataInv[ “amount” ] ) do[/lua]
Is the problem.


ERROR: GAMEMODE:'PlayerInitialSpawn' Failed: HavocBox/gamemode/init.lua:66: 'for' limit must be a number

You should check if tonumber(DataInv[“amount”]) actually returns a number before you use it as a limit in your for loop. There must be a problem reading the file or converting it to a table from KeyValues, try PrintTable’ing the DataInv.

-snip- whoops lol maker

What has that got to do with anything, I’m sure Terabit knows how to construct a for loop.

-snip-

Quick check you think this will work im about to re launch gmod because of a random crash.

[lua]
function meta:LoadPlayerData( )
// Similar to the file.append that is supplied by alzui sleet’s gg module.
local ID = string.gsub( self:SteamID( ), “:”, “_” )
if ( !file.Exists( “Players/” … ID … “/data.txt” ) ) then self:SetBank( 100 ) self:ChatPrint( “Welcome [NEW]” … self:Nick() … " your Garry Points[GP] has been set to 100!" ) self:SavePlayerData( ) end
if ( file.Exists( “Players/” … ID … “/data.txt” ) ) then
local Data = util.KeyValuesToTable( file.Read( “Players/” … ID … “/data.txt” ) )
self:SetBank( tonumber( Data[ “bank” ] ) )
end
if ( file.Exists( “Players/” … ID … “/inventory.txt” ) ) then
local DataInv = util.KeyValuesToTable( file.Read( “Players/” … ID … “/inventory.txt” ) )
for k, v in pairs( DataInv ) do
local args = tonumber( v[ “amount” ] )
for i = 1, ( args ) do
self:ItemAdd( tostring( v[ “class” ] ) )
end
end
end
self:ChatPrint( “Garry Points[GP] is currently in beta!” )
self:ChatPrint( “Welcome " … self:Nick() … " your Garry Point(s)[GP] data was loaded!” )
self:ChatPrint( self:Nick() … " your current balance is " … self:Bank( ) … “[GP].” )
end
[/lua]

Did you even read his code?

He has constructed his for loop correctly.

Tell me what is wrong with this Cubar?

[lua]for i = 1, tonumber( DataInv[ “amount” ] ) do [/lua]

[editline]05:44PM[/editline]

Double post :v:

Did you just suggest maker doesn’t know enough lua to contribute here? He is a lua genius!

Seems I fixed the problem thanks anyways.

DataInv[ “amount” ] isn’t a proper number. The function tonumber returns nil if the value couldn’t be cast to a number, which means DataInv.amount isn’t numeric.

So you can do this:

tonumber( value ) or 0

Which will use 0 if the value can’t be converted.

Also, just so you know, you can use DataInv.amount rather than DataInv[ “amount” ] in virtually every case ( excluding when keys are not proper variable names ).

I didn’t know that thanks for the help.

Sorry i meant you to learn more lua.

:irony:

lol wut, no need to cry now :)?