Video:
I clicked on a part that increases my cash in the leaderstats. It worked, but as soon as I rejoin the game, my cash is back to the original one. The coins originally is 180, but as I clicked the part, it changed to 280. The data wasn’t being saved. The data store testing is in studio, not in a real game.
Code:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local playerCoinsDataStore = DataStoreService:GetDataStore("PlayerDataStore", "PlayerCoins")
Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = plr
local intValue = Instance.new("IntValue")
intValue.Name = "Coins"
intValue.Parent = folder
local coins
local success, currentCoins = pcall(function()
coins = playerCoinsDataStore:GetAsync(plr.UserId)
end)
if success then
intValue.Value = coins
print(coins)
else
warn(currentCoins)
end
end)
Players.PlayerRemoving:Connect(function(plr)
local success, errorMessage = pcall(function()
playerCoinsDataStore:SetAsync(plr.UserId, plr.leaderstats.Coins.Value)
end)
if success then
print(errorMessage)
else
print(errorMessage)
end
end)
Script inside of the part: (On the server)
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
plr.leaderstats.Coins.Value += 10
end)
What does the code for the part that increases your stats look like? Is it in a local script?
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
plr.leaderstats.Coins.Value += 10
end)
At least to me, your script appears to be fine. I believe the issue is probably the clickable part. If the clickable part is changing the coins value via a local script, it will not update on the server side. Ensure that the script changing the value is in an actual script and not a local script.
1 Like
It’s on the server defgfdwswedfvgbvfedwefvg
Yup this is what im thinking as well.
Or do I have to test it in real game? like not inside studio?
How are you exiting the studio test? Are you just pressing stop? Cause i could be wrong here but maybe that doesnt fire PlayerRemoving try just leaving the game inside the studio test instead of pressing stop.
1 Like
Yes, I just pressed stop. fbrfedwdfrrfe
You know what, there are actually times where the data is saved and sometimes it’s not.
Btw I tried to actually leave the game on the client, but it did not work.
Oh wait the plr stats are being held inside the player when the plr gets removed and also deletes the stats. That could be it.
You might want to also save the player data when the game is bound to close (which is what happens when servers are shutdown or you stop the game in studio).
game:BindToClose(function()
for _, plr in pairs(Players:GetPlayers()) do
local success, errorMessage = pcall(function()
playerCoinsDataStore:SetAsync(plr.UserId, plr.leaderstats.Coins.Value)
end)
if success then
print(errorMessage)
else
print(errorMessage)
end
end
end)
2 Likes
Oh wow, this works, so it’s true that the PlayerRemoving event did not run? kinda odd.
Yeah I was going to mention something about this as well PlayerRemoving is good but not always 100% reliable a lot of games do auto saves x amount of time or save data after a action with cool down.
1 Like
But does the PlayerAdded event works fine?
So the data gets saved when the game is shut down, but not when each individual player leaves?
I do believe it’s because you’ve got the leader stats in the plr when I player gets removed it’ll also get rid of the leaderstat so if you want playerremoving to work put leaderstat in replicatedstorage then delete it once the save is successful. Sorry for bad grammar writing this on my phone.
It will still work if each individual player leaves, it’s just that in studio, when you stop the game, it registers as shutting down the server and may not fire the .PlayerLeaving event. You can find more info about :BindToClose here on the official Roblox documentation site.
1 Like
Here are a few things to check and consider:
-
Studio Access to Data Stores:
- 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!
1 Like