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
Just let me know if you need any more informaton.