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)
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