My data doesn't save when player leaves

Hello everyone, i made a save data, but when player leaves doesn’t save, here is the script:

local dataStore = game:GetService(“DataStoreService”)
local data = dataStore:GetDataStore(“Stats”)

game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new(“Folder”,Player)
leaderstats.Name = “leaderstats”

local level = Instance.new("NumberValue",leaderstats)
level.Name = "Level"
level.Value = 0
local savedLevel = data:GetAsync(Player.UserId.."-Level")
if savedLevel then
	level.Value = savedLevel
end

end)

game.Players.PlayerRemoving:Connect(function(plr)

local success,err = pcall(function()
	data:SetAsync(plr.UserId.."-Level")
end)

end)

1 Like

The second argument in SetAsync is missing! Using pcall is great for catching errors, but you still need to check them afterwards. Try adding this afterwards.

local success,err = pcall(function()
	local level = plr:FindFirstChild("Level", true)
	data:SetAsync(plr.UserId.."-Level", level.Value)
end)

if not success then -- check for failure!
    warn(err) -- print the error, and continue running
end

Another issue may arise if you are testing this in studio. It is very often the case that the server shuts down before the player removing call can finish. This community tutorial covers how to solve such issues and other common ones.

1 Like

You need to pass the value you want to save into the second argument of SetAsync

In your case, it might look like this:

game.Players.PlayerRemoving:Connect(function(plr)
	local level = plr:FindFirstChild("leaderstats") and plr.leaderstats:FindFirstChild("Level")
	if level then
		data:SetAsync(plr.UserId.."-Level", level.Value)
	end
end)
4 Likes

You’ll need to add a pcall for GetAsync, and print an error in the output when the Data doesn’t save.

You also need BindToClose so the player’s data saves when the server shutsdown or when they get disconnected.

1 Like

okay, let me add it, thank you for reply!

alright, let me try it, thank you for the reply

can you give me an example, please

it doesn’t seem to work, nothing happen yet

Something like game:bindtoclose(function()
For i, player in ipairs(game:GetService(“Players”):GetPlayers()) do
local level = player:FindFirstChild(“Level”) if level then data:SetAsync(player.UserId…"-Level", level.Value) end end

1 Like

okay, let me tried it, thank you for the reply

1 Like

My apologies, I missed that the level stat was in the leaderstats, not directly in the player. My original reply has been edited with a corrected solution

3 Likes

it works!, thank you so much for your time!

Looks like @gertkeno edited their reply to include the right answer after I messaged them feedback about their original response.

3 Likes

No problem, happy to help. Next you may want to consider saving a table so that you can store more data values without needing to make additional datastore calls. Here’s one way you might do that:

game.Players.PlayerAdded:Connect(function(plr)
    local save = data:GetAsync(plr.UserId.."-Data") or {}

    -- it is actually better to parent after setting the name, otherwise
    -- plr:WaitForChild("leaderstats") may not work correctly
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local level = Instance.new("IntValue")
    level.Name = "Level"
    level.Value = save.Level or 0
    level.Parent = leaderstats

    local money = Instance.new("NumberValue")
    money.Name = "Money"
    money.Value = save.Money or 10
    money.Parent = leaderstats
end

game.Players.PlayerRemoving:Connect(function(plr)
    local leaderstats = plr:FindFirstChild("leaderstats")
    if leaderstats then
        local save = {}
        save.Level = leaderstats:FindFirstChild("Level") and leaderstats.Level.Value
        save.Money = leaderstats:FindFirstChild("Money") and leaderstats.Money.Value
        data:SetAsync(plr.UserId.."-Data", save)
    end
end)

In this example I have stored level as an IntValue, so that it is always a whole number. This can help prevent floating point percision errors.

You can easily add other values to the player’s save file, including other value types such as strings or booleans, by following the same format as what is used for level and money. You should also look into auto-saving and proper error handling as others in the thread have suggested.

Good luck!

4 Likes