I’ve been trying to make an IntValue change to os.time() when the player leaves (which is then supposed to save), however it never seems to work. I*'ve tried both in-studio and in-game, but neither worked.
Script (LocalScript):
game.Players.ChildRemoved:connect(function(player)
print("test")
if (player:IsA("Player")) then
print(player.Name)
local e = os.time()
script.Parent.Parent.Parent.Parent.Data.TimeLeft.Value = e
end
end)
os.time() returns time with microsecond precision, but the time is in seconds so it is not an integer. I would recommend using time() instead, since it returns the number of seconds with second precision, meaning it is an integer.
Did you test with PlayerAdded and with only yourself? PlayerAdded does fire but it won’t fire when you join, only when other people join. Your local scripts will run after you’ve already joined, so by the time you’re in the game and your local scripts are running, it’s too late for PlayerAdded to fire for yourself.
I just tested this in a local script in a 2 player server. PlayerAdded in my local script didn’t fire for when I joined, but it did fire for when other people joined after me.
I’m using PlayerRemoved, not PlayerAdded. I’ve heard that PlayerAdded does not work properly in studio, and I even tried BindToClose in case the server shutdown too quickly. Edit: If I change the IntValue manually, it does save, but os.time/time doesn’t.
Do you have a server script saving the int value to a datastore when you leave? If that’s the case, you’re changing the int value in a local script and saving it with a server script. The server script won’t notice the change you made to the intvalue from the local script. Instead you should be doing everything on the server. I really don’t see the point of you using a local script for this anyway. Hackers can mess it up.