By default, games tested in Studio cannot access data stores. To enable Studio access, follow these steps:
Make sure your game is published (File > Publish to Roblox).
Open the Game Settings window from the Home tab.
In the Security section, turn on “Enable Studio Access to API Services.”
Click “Save” to register your changes².
PlayerRemoving Event Behavior in Studio:
Sometimes the PlayerRemoving event doesn’t fire as expected in Studio. To work around this, try the following:
Run a local test server with two players (you can do this from the Test tab).
Leave the game from one client while keeping the other client connected.
Check the output on the server instance (the first one that opens when testing in the local server).
This way, you can verify if the data is being saved correctly before the server shuts down³.
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!
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 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.
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)
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?
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