Data Does Not Save

Also, when I tested the game with 2 players, this happened when one of them left the game.

And this comes from the print statement of this part of the code.

Btw I’ve changed the PlayerRemoving event to BindToClose. I still don’t get it how BindToClose will save data for each individual when it fires when the game is about to close, but isn’t that for the whole players?

But like, shouldn’t the leaderstats be in the player? Isn’t that how it works?

If you want it to be saved on roblox studio, you must do:

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then
		wait(1)
	end
end)

I think roblox studio closes so fast that player removing don’t get to fire, putting a wait(1) will give enough time

Wait, what exactly is bound functions?

what do you mean by bound function?

Wait, it’s from the documentation about BindToClose. here on the official Roblox documentation site

yea, it is an event that fires when the server is about to close

But it is when a server is about to close and not when a player leaves? so how would that save all the players data. The server closes, so every player leaves at the same time. And so what is bound function?

Wait, put your data save inside PlayerRemoving event, BECAUSE BindToClose only fires when the server is about to close

You know like, when there is no more players inside a server, BindToClose will fire and then the server will close, but when people are leaving the game and there are still remaining players, BindToClose is not gonna fire, ya know, then the players that leave won’t get their data saved.

1 Like

Instead of putting your data save inside BindToClose, put it inside the PlayerRemoving instead.

For BindToClose use:

game:BindToClose(function()
	for _,plr in pairs(game:GetService("Players"):GetPlayers()) do
		plr:Kick() -- kick() will fire the PlayerRemoving event
	end
end)

It is simple with the right information, but if you have misleading informations, things will get confusing.

Wait so am I gonna use both? BindToClose and PlayerRemoving?

Yup, that is what you are suppose to do

So if a player leaves, the PlayerRemoving function will run. But BindToClose is also used for precaution when the game closes down and forces everyone to leave. But what if the PlayerRemoving event does not fire?

Correct, you can put it that way, it will fire when players leave or else PlayerRemoving would defeat that purpose

What if the PlayerRemoving event does not fire? or will it fire in game and usually not firing in studio?

I think it will usually fire in game, but for roblox studio it is buggy

What is this for? wfweqerefrwerfw

Oh, that just checks if you are currently using Roblox Studio, and if so, wait(1) so that roblox studio gives enough time for PlayerRemoving to fire and save your data

1 Like

Also I think there will be a rare occurrence of when PlayerRemoving event and BindToClose will run at the same time. But even if it does, I think it will work just fine.