Data Does Not Save

Here are a few things to check and consider:

  1. Studio Access to Data Stores:

    • By default, games tested in Studio cannot access data stores. To enable Studio access, follow these steps:
      1. Make sure your game is published (File > Publish to Roblox).
      2. Open the Game Settings window from the Home tab.
      3. In the Security section, turn on “Enable Studio Access to API Services.”
      4. Click “Save” to register your changes².
  2. PlayerRemoving Event Behavior in Studio:

    • Sometimes the PlayerRemoving event doesn’t fire as expected in Studio. To work around this, try the following:
      1. Run a local test server with two players (you can do this from the Test tab).
      2. Leave the game from one client while keeping the other client connected.
      3. Check the output on the server instance (the first one that opens when testing in the local server).
      4. This way, you can verify if the data is being saved correctly before the server shuts down³.
  3. Using game:BindToClose():

    • To ensure data saving even in Studio, consider using game:BindToClose(func). This function runs before the server shuts down, allowing you to save data reliably¹.

If you encounter any further issues, feel free to ask for more assistance!

1 Like

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