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

How do I wrap this script in a pcall? Do I need to add any other things to make it work better?

Asked by
2Loos 168
4 years ago
--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.

0
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 JailBreaker_13 350 — 4y
0
Ok 2Loos 168 — 4y
0
Would this work game:BindToClose(function(player) DatastoreService:SetAsync(player.UserId, player.leaderstats["Minutes Flown"].Value) end) 2Loos 168 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

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:

  • A boolean, determining if the function successfully ran. This will be false if the function errors out.
  • The error of the function, if the boolean described above is false.

The following services can be compliant with pcall() specifically because they are prone to errors:

  • DataStoreService, and all DataStores
  • MarketplaceService

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

0
It would help if you included an example, but still, much appreciated! 2Loos 168 — 4y
0
The usage of pcall() is fairly straightforward. DeceptiveCaster 3761 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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.

Answer this question