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

Why does this sometimes saves when the player leaves but mostly does not?

Asked by
tjtorin 172
5 years ago

I am trying to make the datastore save the players time and it loads in data fine but when the player leaves most of the time it does not save.

local m = require(workspace:WaitForChild("MainModule"))
local DSS = game:GetService("DataStoreService")
local timeStore = DSS:GetDataStore("PlayerTime_v1")

game.Players.PlayerAdded:Connect(function(player)
    local success, playerTime = pcall(function()
        return timeStore:GetAsync("UID_"..player.Name)
    end)

    if success then
        print("Succefully loaded data!")
        m.currentTime = playerTime
        while wait(1) do
            m.currentTime = m.currentTime + 1
            print(m.currentTime)
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, err = pcall(function()
        timeStore:SetAsync("UID_"..player.Name,m.currentTime)
    end)

    if success then
        print("Data saved!")
    end
end)
0
Are you testing this live or on studio? DinozCreates 1070 — 5y
0
studio tjtorin 172 — 5y

Answer this question