Need help with function

[lua]
function TrackerCheck( ply )
local TRACKEDGROUPS = string.Explode( “::”, ( file.Read( “AT_groups.txt” ) ) )
local TRACKEDPLAYERS = string.Explode( “::”, (file.Read( “AT_players.txt” ) ) )
local pID = ply:UniqueID()
ServerLog(“Begin Update”)
if table.HasValue( TRACKEDGROUPS, ply:GetUserGroup() ) or table.HasValue( TRACKEDPLAYERS, pID ) and not ply:IsConnected() and ply.starttime ~= nil then
local len = os.time() - ply.starttime
sql.Query( “UPDATE session_time SET sessionlength = '”…len…"’ WHERE starttime = ‘"…ply.starttime…"’ AND unique_id = ‘"…ply:UniqueID()…"’" ) --update their current active session

	prevtotal = sql.Query( "SELECT totaltime FROM total_time WHERE unique_id = '"..ply:UniqueID().."'" ) --update their totaltime entry
	local newtotal = prevtotal[1]["totaltime"] + len
	sql.Query( "UPDATE total_time SET totaltime = '"..newtotal.."', usergroup = '"..ply:GetUserGroup().."', lastonline = '"..os.time().."' WHERE unique_id = '"..ply:UniqueID().."'" )
	ServerLog("Success")
end

end
timer.Create( “TrackerUpdateTimer”, 60, 0, function() TrackerCheck( ply ) end )
[/lua]

im always getting “tried to index ply in nil value” but why? its just one function im trying to add into script.
all code in script who has ply:stuff() in it works perfectly
I know i need to specify what ply is but how the hell it works without doing it in all code except my function?
Any suggestion how to do it? its server side function.

What line exactly causes the error? What exactly is the error?

3rd line.

“Attempt to index local ‘ply’ (a nil value)”

or if i removed 3rd line it will show error in first next line with ply in it

“Attempt to index ‘ply’ (a nil value)”

Your timer is passing ply to the function but I don’t see anywhere that actually defines what ply is.

im new to lua and i already said that i know i need to specify what ply is

i tried [lua]local ply = LocalPlayer()[/lua] it dosent work and whole script stops working with this.

also like i said for some reason everything else works without specifying this confuses me most.

I assume that you are running this serverside and you want this to run for all players?

actualy it suposed to take players from sqlite table and only execute stuff after “then” if theyre not connected to server (i used “and not ply:IsConnected()”)

You can’t get a player object if he is not on a server.

[editline]31st January 2014[/editline]

You probably want to run this function from PlayerDisconnect, Shutdown or PlayerInitialSpawn hooks.

[editline]31st January 2014[/editline]

Not from a timer.

i dont need player object. it should take all tracked players and if player is playing it should do nothing. but if he is not on server it should end their session. UTime does something like this.

That’s not how you do it.
You start the session in PlayerInitialSpawn and end it in PlayerDisconnect and Shutdown.

its already done. but if server crashes? it dosent call d/c hook on anyone nor shutdown.

Then you must cycle through all players in your timer and call it for every player.



timer.Create( "TrackerUpdateTimer", 60, 0, function()
    for id, ply in pairs(player.GetAll()) do
        TrackerCheck( ply )
    end
end )

Actualy this is good idea. thanks