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

Why doesn't my data save to the datastore on leave or server shutdown?

I have another script that saves every 2 minutes, so I know that the save script does work. It just doesn't save when someone leaves or the game closes.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DS = game:GetService("DataStoreService"):GetDataStore("Data")

function save(player)
	local data = DS:GetAsync(player.UserId)
	data["tokens"] = player:FindFirstChild("Tok").Value
	data["combatLogged"] = player:FindFirstChild("CombatLogged").Value
	DS:SetAsync(player.UserId, data)
	print("Data Saved!")
end

Players.PlayerRemoving:Connect(function(player)
	wait(3)
	save(player)
	print("Saved "..player.Name.."'s data!")
end)

game:BindToClose(function()
	wait(1)
	for i, player in ipairs(Players:GetPlayers()) do
		coroutine.wrap(save)(player)
		print("Saved "..player.Name.."'s data!")
	end
end)

2 comments

0

Remove the wait, I think that should help

0

The wait is so that the script doesn't instantly end, there is a max amount of time that the client is able to stay open when leaving the game and it is well under that.

1 answer

✓ Accepted answer

Remove the wait on both BindToClose and PlayerRemoving. There is a point where the Roblox client/server will not longer respond on shutdown if the client/server was shutdown illegally (Not actually law illegally. But illegal by the computer. Powering off the computer, crashing, task manager, etc)

If you use the ESC menu, and leave the game roblox will correctly save since the client yields on PlayerRemoving. But when you illegally close the application Windows/linux will force stop the application causing it to shutdown quicker than usual.

1 comment

0

ty idk why the thing i saw said to add the wait it said it would close too fast if i didnt

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