Ad
Log in to vote
0

Datastorage does not save data when leaving, how to fix?

Asked by 13 days ago

I know someone else has asked this but it hasn't been answered and I'm really stuck and worried I won't be able to find it again. So sometimes my data stores work and sometimes they don't. It's weird. Here's the important code and I'm not getting any errors or errormessage

game.Players.PlayerRemoving:Connect(function(player)
    local Id = "player_"..player.UserId
    local Data = player.leaderstats.Cash.Value

    local success, errormessage = pcall(function()
        DataStore1:SetAsync(Id, Data)
        print("DataSaved")
    end)

    if success then
        print("DataSaved")
    elseif errormessage then
        print("There was a error")
        warn(errormessage)
    end
end)

0
Sorry. I mix things up easily.. I dont know what is wrong with your code then. bobifart 1 — 13d
0
nah it's fine Dragon_meck 4 — 13d
0
if you are testing the script in studio, try to test your game in the roblox's website instead 7777ert 12 — 13d

1 answer

Log in to vote
0
Answered by 13 days ago
Edited 13 days ago

If you are testing and you are the only one in you server, the server shuts down when you leave and the code wont run. To prevent this, add another block of code that includes this

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do

        local Id = "player_"..player.UserId
        local Data = player.leaderstats.Cash.Value

            local success, errormessage = pcall(function()
            DataStore1:SetAsync(Id, Data)
                print("DataSaved")
            end)

        if success then
                print("DataSaved")
        elseif errormessage then
            print("There was a error")
warn(errormessage)
        end
    end
end)

Put this under your other code

0
thanks:) Dragon_meck 4 — 13d
Ad

Answer this question