Data Store not saving after leaving

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)

game.Players.PlayerRemoving:Connect(onPlayerExit)

4 Likes

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

BindToClose Documentation

3 Likes

i tested it on studio and in game and it not saving

1 Like

Is there anything inside the output?

1 Like

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

1 Like

but i tested it in game and in studio and it not saving data

You should save on both events, player removing doesn’t work when the server is being shutdown

1 Like

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.

2 Likes

Yeah, that’s what I meant by using RunService to detect which environment is being used as a host.

So the script can decide between PlayerRemoving and game:BindToClose()

1 Like

You could use something like this:

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
2 Likes
game.Players.PlayerRemoving:Connect(function(plr)
 -- Save for plr
end)

game:BindToClose(function()
-- Save for all
end)

You shouldnt check if its studio

1 Like

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.

No, when you use the shutdown server button on the website. The player removing events don’t run. And that will result in data loss.

It’s not saving the data twice? When as i said shutting down a server doesn’t run player removing events

2 Likes

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.

1 Like

i used :BindToClose() and now its saving

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.