--Leaderstats script for new flight simulator
local DatastoreService = game:GetService("DataStoreService"):GetDataStore("FlightwareDSS")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local GROUPRANK = Instance.new("StringValue",leaderstats)
GROUPRANK.Name = "Rank"
GROUPRANK.Value = player:GetRoleInGroup(8323568)
local MINUTESFLIED = Instance.new("IntValue", leaderstats)
MINUTESFLIED.Name = "Minutes Flown"
MINUTESFLIED.Value = DatastoreService:GetAsync(player.UserId) or 0
end)
game.Players.PlayerRemoving:Connect(function(player)
DatastoreService:SetAsync(player.UserId, player.leaderstats["Minutes Flown"].Value)
end)
I have heard that wrapping this in a pcall is a good idea. How do I do that? Is there anything else I should consider adding? Is there a way to autosave incase the player gets kicked. I ask because apparently when the server shuts down, the PlayerRemoving function doesn't occur.

3 comments
I don't experience on how to do this, but I do know what pcall does. Pcall can tell you if it saved successfully or not, that way, you can try saving again if it fails, or even warn the player that data is not saving. To fix your "PlayerRemoving" issue, you use game.BindToClose as another event. When the last player leaves, it does not fire PlayerRemoving, it fires game.BindToClose
Ok
Would this work game:BindToClose(function(player) DatastoreService:SetAsync(player.UserId, player.leaderstats["Minutes Flown"].Value) end)