Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why isn't this DataStore script working and am I doing it correctly?

I want to save the time in the game and this script is not working. I have turned on API Services. Code:

local ds = game:GetService("DataStoreService")
local data = ds:GetDataStore("ClockTime")

game.Players.PlayerAdded:Connect(function(p)
	local success, err = pcall(function()
		game.Lighting.ClockTime = data:GetAsync(p.UserId)
	end)
	if success then
		print("yay")
	end
	if err then
		print("err: "..err)
	end
end)

game.Players.PlayerRemoving:Connect(function(p)
	local success, err = pcall(function()
		data:SetAsync(p.UserId, game.Lighting.ClockTime)
	end)
	if success then
		print("yay")
	end
	if err then
		print("err: "..err)
	end
end)

When I am doing this in game, it just sets it to the time in the game and not the DataStore save.

Thanks, b_mni.

1 answer

✓ Accepted answer

The problem with Players.PlayerRemoving is that it does not count the last player in the server. Roblox recommends using game:BindToClose() as it will operate 30 seconds before the server shuts down.

2 comments

0

ok i will try that. BUT: how am I supposed to do that. By the way, it's 1 player servers

0

perfect I will experiment with it more to my likings.

This discussion is preserved as it was. New answers, comments, and votes are closed.