Issue with OS.Time and Client/Server Script

Hi, sorry this is my first time posting here so if it’s the wrong place please let me know!

I am trying to put together a script to track the user’s playtime. This is what I have put together:

Client -

playerTimePlayed = {}
tempJoin = {}
RegisterCommand('stats', function(source, args, user)
	TriggerServerEvent('CheckTimePlayed')
	while(playerTimePlayed[source] == nil or tempJoin[source] == nil) do
		Wait(10)
	end
	TriggerEvent('chat:addMessage', {
		args = {":: Stats ::"},
		color = {0, 255, 255}
	})
	local temp = math.floor(playerTimePlayed[source]/3600)
	local temp2 = math.floor((playerTimePlayed[source]-(temp*3600))/60)
	TriggerEvent('chat:addMessage', {
		args = {": Time Played:^7 " .. temp .. " hours, " .. temp2 .. " minutes and " .. (playerTimePlayed[source]-(temp*3600)-(temp2*60)) .. " seconds."},
		color = {0, 255, 255}
	})
end)

Server -

AddEventHandler('playerConnected',function(source)
	playerTimePlayed[source] = 0
	tempJoin[source] = os.time()
end)
AddEventHandler('playerDropped', function(source)
	playerTimePlayed[source] = playerTimePlayed[source] + (os.time() - tempJoin[source])
end)
RegisterServerEvent('CheckTimePlayed')
AddEventHandler('CheckTimePlayed',function(source)
	if(tempJoin[source] == nil) then 
		tempJoin[source] = os.time()
	end
	if(playerTimePlayed[source] == nil) then 
		playerTimePlayed[source] = (os.time() - tempJoin[source])
	end
end)

So I have been getting an error in my script that says that I am attempting to perform arithmetic on a nill value, referencing this line:

local temp = math.floor(playerTimePlayed[source]/3600)

This happens when I have commented out the 3 or so lines which check for a nill value. When I do not comment these out, it just gets stuck in the ‘Wait’ loop. My conundrum is that the code is not working and I cannot fathom as to why; CheckTimePlayed should be updating the playerTimePlayed with the right info but it doesn’t seem to be doing so. Am I missing something obvious?

I have been using a server event to check the time as os.time doesn’t work in client-sided scripts from what I have seen.

Any help is much appreciated.

Thanks

1 Like

Sorry for bumping but any ideas about this?

Sorry to bump again but any ideas on this issue?

Bumping again, any ideas?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.