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

Why my data saving scripts sometimes work but most of times not?

Asked by 4 years ago

local dataStoreService = game:GetService("DataStoreService")

local timeData = dataStoreService:GetDataStore("TimeData")

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local Time = Instance.new("IntValue",leaderstats)
Time.Name = "Time"
local success , getValue = pcall(function()
    return timeData:GetAsync(player.UserId) or 0
end)
if success then
    print("the load was successful")
else
    warn(getValue)
end
Time.Value = getValue
print ("Loaded Value "..getValue)

game.Players.PlayerRemoving:Connect(function(player)
    local succes , erra = pcall(function()
        timeData:UpdateAsync(player.UserId,function(wtf)
            if Time.Value > wtf then
                return Time.Value
            end
        end)
    end)
    if not succes then
        warn(erra)
    end
    print(player.Name.." has left the server".."("..player.UserId..")")
end)

end)

1 answer

Log in to vote
0
Answered by 4 years ago

PlayerRemoving event as datastore saving is unreliable when the last player leaves (without game:BindToClose()). especially when u test it out in studio

playerRemoving doesnt work in the following conditions: - The last player leaves - The developer manually shut them down via the game page - A network error is encountered and the server is forced to shut down

see this article how to properly save

0
I love u TerranRecon 49 — 4y
0
thanks, if this helped you can mark this as answer :D VerdommeMan 1479 — 4y
Ad

Answer this question