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

Using pcall with Datastore?

Asked by
dirk2999 103
5 years ago

Everything I have read pcall should be used with Datastore because Datastore tends to error, I just started creating my script, but not exactly sure how to incorporate pcall into my script. I haven't started the saving portion of the script because I am going to be adding and saving many variables.

local DataStore = game:GetService("DataStoreService")
local dataPoints = DataStore:GetDataStore("testPoints")

game.Players.PlayerAdded:connect(function(plr)
    local points = Instance.new("IntValue",plr)
    points.Name = "Points"
    points.Value = dataPoints:GetAsync(plr.userId) or 0
end)
0
What is the "or 0" for? KennySfromTitan 106 — 5y
0
If the GetAsync returns nothing, it sets the value to 0 dirk2999 103 — 5y
0
If I helped you would you accept my answer? User#21908 42 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

You can just surround the areas that you are loading and saving data with a pcall. The way you can do this is thus:

local success, message = pcall(function() --[[ what I am doing here is giving two names(they can be anything you want) to the two values that pcall will return. the first one success will return a Boolean value if the code inside of the pcall runs successfully(which is why the name is used) then it will return true if not it will return false. The second one message is a string which is the error message if the code fails. Otherwise it will return nil. ]]--
-- just put your code in here
end)

You can use the variables to notify the player if you want to tell them that their data saved or failed to save. Or you can just print it to the output. It is all up to you. That is why it is so useful. Hope this helps and have a great day scripting!

Ad

Answer this question