My Data Store not saving after leaving the game i dont know what’s wrong
this is code:
local DataStoreService = game:GetService(“DataStoreService”)
local playerData = DataStoreService:GetDataStore(“PlayerData”)
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Taps = Instance.new("IntValue")
Taps.Name = "Taps"
Taps.Parent = leaderstats
local Gems = Instance.new("IntValue")
Gems.Name = "Gems"
Gems.Parent = leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirths"
Rebirths.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId
local data = playerData:GetAsync(playerUserId)
if data then
Taps.Value = data['Taps']
Gems.Value = data['Gems']
Rebirths.Value = data['Rebirths']
else
Taps.Value = 0
Gems.Value = 0
Rebirths.Value = 0
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player)
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats)
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
Well, I presume that you only tested it in studio. I don’t know specifically why this occurs, but try using the RunService to detect when the server is running on Studio and when it is not. When it is running on studio, try saving the data with the game:BindToClose() function instead of the PlayerRemoving function. When it is not running on studio use PlayerRemoving
The issue is that Roblox studio shuts down game with your player leaving, meaning there is no timeframe where it can save.
Your code looks all good, and should work when you test it without roblox studio
If you tried it in roblox player and it didn’t save, try doing game:BindToClose and save the data there.
The issue might be that when you are the last person leaving the game, the server closes before it can save data… I don’t think that should occur, but if you are having issues with it like that, then it probably is.
local RunService = game:GetService("RunService")
local PlayerService = game:GetService("Players")
if not RunService:IsStudio() then
-- // Safe with `PlayerRemoving`
else
game:BindToClose(function()
for _,player in PlayerService:GetPlayers() do
-- // Now use the same data-saving function
end
end)
end
Well, I don’t see a problem why not?
After all using :BindToClose() for saving data, is because studio specifically is making that problem, also technically speaking, you are saving the data of the last player twice. I don’t know if this can lead to further issues, therefor I always use RunService to check which is the environment host.
If you forcefully shutdown servers, then any currently unsaved data will be lost, meaning you should include caching system which saves data periodically, but this is out of the scope of original post, so I feel like we should end it here.