timer failed!

Hi all

I’m hoping one of you can help me with this. I have a timer that is failing after a couple of times:



saved pos...
saved pos...

[ERROR] attempt to call a table value


Timer Failed! [Simple][@gamemodes/futurplay/gamemode/sv_functions.lua (line 68)]




here’s the code. fp_query is a function to perform an SQL query



local location_save_frequency = 5

--autosave the player's location every x seconds
function fp_save_location(ply)


    --only run if the persona has been set up
    if ply._persona then
    
        local current_pos = tostring(ply:GetPos())
        
        local query1 = {}
        query1["datatype"] = "persona"
        query1["id"] = ply._persona["id"]
        
        local query2 = fp_query("find", query1)
        query2 = query2[1]
        query2["location"] = current_pos
        
        fp_query("update", query1, query2)
        
        debug_text("saved pos...")
    
    end
    
    --run the save
    timer.Simple(location_save_frequency, function() fp_save_location(ply) end)


end

Pretty sure what happens is that ply becomes NULL and thus the timer fails. Add IsValid( ply ) check on the first line of the function.

Also saving data every 5 seconds is kind of overkill if you ask me.

-snip-


(User was permabanned for this post ("Alt of permabanned user, getting banned on alts once again" - postal))

Cheers man :slight_smile: You were right. These changes fixed the issue:



--autosave the player's location every x seconds
function fp_save_location()

	--for each player in the server
	for k,v in pairs(player.GetAll()) do
	
		--only run if the persona has been set up
		if v._persona then
		
			--current pos
			local current_pos = tostring(v:GetPos())
			
			--get the current Persona
			local query1 = {}
			query1["datatype"] = "persona"
			query1["id"] = v._persona["id"]
			
			--update the pos
			local query2 = fp_query("find", query1)
			query2 = query2[1]
			query2["location"] = current_pos
			
			--and then save it
			fp_query("update", query1, query2)
			
			debug_text("saved pos for player " .. v:Nick() )
		
		end
		
	end
	
	--run the save
	timer.Simple(location_save_frequency, function() fp_save_location() end)

end
fp_save_location()

5 seconds was just for testing but… if it’s not killing the CPU I’ll just leave it at 5 seconds. The gamemode I’m building should hardly use any CPU anyhow :slight_smile:

[editline]24th August 2014[/editline]

This method, while inefficient, ensures that the player’s location is saved even if the server crashes or loses power. To me that’s worth it if it doesn’t cause issues