--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.
pcall()
is a function used for error catching and preventing the script from breaking if the function wrapped inside of the pcall()
errors out. pcall()
takes a function as its only argument and returns two values:
The following services can be compliant with pcall()
specifically because they are prone to errors:
When using DataStoreService:GetAsync()
it is generally an excellent idea to wrap it in a function, wrapped in pcall()
. This is because if the DataStore fails to give information on the key, the function will error out, and if that is not in a protected call (pcall), the script will break.
The same should apply to SetAsync()
.
This only saved the data, doesn't load it. https://www.youtube.com/watch?v=h92s4Xn26oc&t=311s Watch this video for more info and fixing this script.