Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why doesn't my data save to the datastore on leave or server shutdown?

Asked by 2 years ago

I have another script that saves every 2 minutes, so I know that the save script does work. It just doesn't save when someone leaves or the game closes.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DS = game:GetService("DataStoreService"):GetDataStore("Data")

function save(player)
    local data = DS:GetAsync(player.UserId)
    data["tokens"] = player:FindFirstChild("Tok").Value
    data["combatLogged"] = player:FindFirstChild("CombatLogged").Value
    DS:SetAsync(player.UserId, data)
    print("Data Saved!")
end

Players.PlayerRemoving:Connect(function(player)
    wait(3)
    save(player)
    print("Saved "..player.Name.."'s data!")
end)

game:BindToClose(function()
    wait(1)
    for i, player in ipairs(Players:GetPlayers()) do
        coroutine.wrap(save)(player)
        print("Saved "..player.Name.."'s data!")
    end
end)
0
Remove the wait, I think that should help msculenny 42 — 2y
0
The wait is so that the script doesn't instantly end, there is a max amount of time that the client is able to stay open when leaving the game and it is well under that. OwOShiba 78 — 2y

1 answer

Log in to vote
1
Answered by
Lakodex 711 Moderation Voter
2 years ago

Remove the wait on both BindToClose and PlayerRemoving. There is a point where the Roblox client/server will not longer respond on shutdown if the client/server was shutdown illegally (Not actually law illegally. But illegal by the computer. Powering off the computer, crashing, task manager, etc)

If you use the ESC menu, and leave the game roblox will correctly save since the client yields on PlayerRemoving. But when you illegally close the application Windows/linux will force stop the application causing it to shutdown quicker than usual.

0
ty idk why the thing i saw said to add the wait it said it would close too fast if i didnt OwOShiba 78 — 2y
Ad

Answer this question