Weird issue regarding a timer

I’m converting a rewards system of mine to make it more optimised. Here’s a rundown of how it works:

  • Player joins the game. An instance value is set up inside of them which holds how long it was between when they left the game and joined back. However long they spent in the previous server is added to this.
  • A join time value is stored in a server script.
  • When the player leaves, however long they spent in the server is stored.
  • When the player attempts to claim a reward every 4 hours, a script will get however long it was between when they last claimed the reward. If it’s greater than 4 hours, they get the reward.

The Issue

  • This timer randomly resets itself, it’s a simple maths logic error but I can’t seem to figure out what’s going wrong.

this stores the join time

joinTimes[player.Name] = os.time()

this resets the value

local function resetTimer(playerName:string)
	joinTimes[playerName] = os.time() --reset the timer
	
	local player = players:FindFirstChild(playerName)
	if player then
		player.TimePlayed.Value = 0
	end
end

this bindable gets the time difference

local function getTimePlayed(name:string)
	local player = players:FindFirstChild(name)
	if not player then return 0 end

	return ((os.time() - joinTimes[name]) + player.TimePlayed.Value)
end

this code calculates how long the player went from when they join

--playerData.Timer is the os.time() value of when the player left
--playerData.Played is how long the player spent in the previous server (this is reset when they claim the free reward)
math.floor((os.time() - playerData.Timer) + playerData.Played)

I’m really confused, any help is appreciated :smiley:
Just let me know if you need any more informaton.

1 Like

Ok I managed to fix it I was right it was a simple maths error. I forgot to add the previous server time which meant it was lost and the system would only work if the player stayed offline for 4 hours.

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