• attempt to index global 'ply' (a nil value) ?
    9 replies, posted
lua.51 attempt to indelx global 'ply' (a nil value) Again, I'm probably an idiot, overlooking some thing of infinite simplicity, but take a look anyway: [lua] timer.Create( "xp_save_timer", 10, 0, function() local xp = ply:GetNWInt("playerxp") - Line 51 here local cur = ply:GetNWInt("currentlevel") local nextlvlxp = ply:GetNWInt("nextlevelxp") sql.Begin() sql.QueryValue("UPDATE xp_player_data SET playerxp = "..xp.."WHERE steam = "..SQLStr(ply:SteamID())..";") sql.QueryValue("UPDATE xp_player_data SET currentlevel = "..cur.."WHERE steam = "..SQLStr(ply:SteamID())..";") sql.QueryValue("UPDATE xp_player_data SET nextlevelxp = "..nextlvlxp.."WHERE steam = "..SQLStr(ply:SteamID())..";") sql.Commit() end ) [/lua]
Can you post the entire script?
[QUOTE=UberMensch;30465350]Can you post the entire script?[/QUOTE] [lua] levelups = { 0, 500, 1200, 2000, 2900, 3900, 5000, 6200, 7500, 9000,} //Create the base sql table sql.Query("CREATE TABLE IF NOT EXISTS xp_player_data('steam' TEXT NOT NULL, 'playerxp' INTEGER NOT NULL, 'currentlevel' INTEGER NOT NULL, 'nextlevelxp' INTEGER NOT NULL,PRIMARY KEY('steam'));") // //Load or create xp variables on player initial connection hook.Add("PlayerInitialSpawn","RegisterXP",function(ply) if not tonumber(sql.QueryValue("SELECT playerxp FROM xp_player_data WHERE steam = "..sql.SQLStr(ply:SteamID())..";")) then sql.Query("INSERT INTO xp_player_data VALUES ("..SQLStr(ply:SteamID())..",".. 0 ..",".. 1 ..","..levelups[1]..");") ply:SetNWInt("playerxp",0) ply:SetNWInt("currentlevel",1) ply:SetNWInt("nextlevelxp",levelups[1]) print("FUCK THIS") else sql.Begin() ply:SetNWInt("playerxp",tonumber(sql.QueryValue ("SELECT playerxp FROM xp_player_data WHERE steam = "..SQLStr(ply:SteamID())..";"))) ply:SetNWInt("currentlevel",tonumber(sql.QueryValue ("SELECT currentlevel FROM xp_player_data WHERE steam = "..SQLStr(ply:SteamID())..";"))) ply:SetNWInt("nextlevelxp",tonumber(sql.QueryValue ("SELECT nextlevelxp FROM xp_player_data WHERE steam = "..SQLStr(ply:SteamID())..";"))) sql.Commit() end end) // timer.Create( "xp_save_timer", 10, 0, function() local xp = ply:GetNWInt("playerxp") - Line 51 here local cur = ply:GetNWInt("currentlevel") local nextlvlxp = ply:GetNWInt("nextlevelxp") sql.Begin() sql.QueryValue("UPDATE xp_player_data SET playerxp = "..xp.."WHERE steam = "..SQLStr(ply:SteamID())..";") sql.QueryValue("UPDATE xp_player_data SET currentlevel = "..cur.."WHERE steam = "..SQLStr(ply:SteamID())..";") sql.QueryValue("UPDATE xp_player_data SET nextlevelxp = "..nextlvlxp.."WHERE steam = "..SQLStr(ply:SteamID())..";") sql.Commit() end ) //Actual leveling function LevelUp(player) player:SetNWInt("currentlevel",(player:GetNWInt("currentlevel")+1)) player:SetNWInt("nextlevelxp",levelups[(player:GetNWInt("currentlevel")+1)]) player:SetHealth(1000) print(player:Name()) end if ply:GetNWInt("playerxp") >= levelups[ply:GetNWInt("currentlevel")+1] then LevelUp(ply) end // //Gain xp hook.Add("PlayerDeath","PlayerDeathTest",function() ply:SetNWInt("playerxp",(ply:GetNWInt("playerxp") + 100)) print("Exp "..tonumber(ply:GetNWInt("playerxp"))) end) // ply:SetRunSpeed(500+(ply:GetNWInt("currentlevel")*10)) [/lua]
The problem is with your timer, the "ply" object doesn't exist until it's declared. I'm guessing your timer is to save everyone's details at once? My Lua is extremely rusty after doing a year's worth of non-stop PHP so I'm not 100% sure on the "for" syntax, but I believe this should work... [lua] timer.Create( "xp_save_timer", 10, 0, function() for _, ply in pairs( player.GetAll() ) do local xp = ply:GetNWInt("playerxp") local cur = ply:GetNWInt("currentlevel") local nextlvlxp = ply:GetNWInt("nextlevelxp") sql.Begin() query = "UPDATE `xp_player_data` SET playerxp = '" .. xp .. "', `currentlevel` = '" .. cur .. "', `nextlevelxp` = '" .. nextlvlxp .. "' WHERE `steam` = '" .. SQLStr( ply:SteamID() ) .. "';" sql.QueryValue( query ) sql.Commit() end end ) [/lua] [editline]15th June 2011[/editline] Fixed the "for" syntax above, it was a godawful mix of PHP and Lua...
Line 44: did not pass "ply" as function argument.
Alright cheers guys; I'll give it a whirl. After whirling: ... This line right here [lua] if ply:GetNWInt("playerxp") >= levelups[ply:GetNWInt("currentlevel")+1] then [/lua] is being given the error [quote] attempt to index global 'ply' (a nil value) [/quote] Again I fail to see what's wrong with the line (and script) [sadface]
[QUOTE=TempestUK;30471793]Alright cheers guys; I'll give it a whirl. After whirling: ... This line right here [lua] if ply:GetNWInt("playerxp") >= levelups[ply:GetNWInt("currentlevel")+1] then [/lua] is being given the error Again I fail to see what's wrong with the line (and script) [sadface][/QUOTE] 69-71 seem to just be randomly placed in the script. They're not inside any function, so thus, ply is not defined. Perhaps you want them in a think function, inside a players loop? Infact, some other of your functions aren't defining "ply", but still using it. (81-85). 91 also seems to be as randomly placed as 69-71. Perhaps you want this on PlayerSpawn?
I have create this in the last 5 minutes. I did not test this but it must work :D [code] |Royal| CreateConVar("exp_inc","10",true,false) CreateConVar("exp_start","100",true,false) exp_start = GetConVarNumber("exp_start") exp_inc = GetConVarNumber("exp_inc") function CalcExpInc(level) local exp if(level<= 1) then exp = exp_start else exp = ((level*exp_inc)+exp_start) return exp end end function CalcLevelUp(lvl,exp) local level_inc = 0 local stop = 0 while(stop == 0) do exp_req = CalcExpInc(lvl+level_inc) if(exp > exp_req) then level_inc = level_inc + 1 //level_inc++ exp = exp - exp_req else if(exp == exp_req) then level_inc = level_inc + 1 //level_inc++ end stop = 1 end end return Level_inc end [/code]
Got it working, finally; Cheers for all your help guys. The problem was how jimbo described it, tah again.
[I]/me is copy and pasting and putting it in to his darkrp edit[/I] No.
Sorry, you need to Log In to post a reply to this thread.