Can't write os.time() to an IntValue?

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.

No difference, the IntValue remains at 0.

Hmm. Does the script print test and the player’s name?

No, it doesn’t. (doingthisfor30chars)

Try using PlayerRemoving instead of ChildRemoved. You can also remove the if statement with that.

My script is a LocalScript though, and PlayerRemoving only runs on the server.

Are you sure it only runs on the server? I don’t see any reason why it shouldn’t run on the client too.

According to this post, no. Edit: Technically it could work since it’s parented under all players. Let me give it a try.

PlayerRemoving and PlayerAdded do fire in local scripts. I just tested it in studio.

Didn’t fire for me in-studio or in-game.

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.

Tried to do it via the server (inside the DataStore), didn’t work either. I can send the DataStore script if needed.

I’m pretty sure time() only works on the client. Try using os.time instead since youre doing it from a server script.

Seems to have done the trick. Thank you.

Edit: If anyone has the same problem, it’s likely because its on the client.

1 Like