Saving data on PlayerRemoving not working

Recently i tried saving the data when the PlayerRemoving event gets triggered but it simply doesn’t work at all, i’ve been trying to fix this for a week now but i can’t find any possible way, the thing is, that it doesn’t save the coins value but neither does it warn: Cannot save data for player, please help.

local CoinsKey = "-Coins"
local CoinsDataStore = game:GetService("DataStoreService"):GetDataStore("CoinsDataStore")

local function SaveCoins(Player)
    local tries = 0    
    local success
    repeat
        tries = tries + 1
        success = pcall(function()
            CoinsDataStore:SetAsync(Player.UserId..CoinsKey,Player.PrivateStats.Coins.Value)
        end)
    until tries == 3 or success
    if not success then
        warn("Cannot save data for player!")
    end
end
game.Players.PlayerRemoving:Connect(SaveCoins)
game.Players.PlayerRemoving:connect(function(SaveCoins) end)

Not sure if this matters… just something I noticed.

(Look at yours then look at mine)

If you are testing this in roblox studio, you should include a bind to close function, since studio is infamous for shutting down before data can save.

I know what you mean but on other scripts it worked that way aswell

Did you GetAsync() for whenever a player joins and gave them their coins stored in the GlobalDataStore?

You should add BindToClose in your script.

1 Like