local DSS = game:GetService("DataStoreService")
local moneyDataStore = DSS:GetDataStore("ExampleDataStore", money)
game.Players.PlayerAdded:connect(function(plr)
scope = "User_"..plr.UserId
local stats = Instance.new("Folder", plr)
stats.Name = "leaderstats"
stats.Value = moneyDataStore:GetAsync(scope)
-- I would not normally do leaderboards this way this is just an example
--how would I wrap this Get Async in a pcall
✓ ANSWER ACCEPTED
How do I use pcall properly?
1 answer
✓ Accepted answer
local DSS = game:GetService("DataStoreService")
local moneyDataStore = DSS:GetDataStore("ExampleDataStore", money)
game.Players.PlayerAdded:connect(function(plr)
scope = "User_"..plr.UserId
local stats = Instance.new("Folder", plr)
stats.Name = "leaderstats"
local success, message = pcall(function()
stats.Value = moneyDataStore:GetAsync(scope)
end)
if success then
-- code
else
print('Datastores have failed, error: '..message)
end
end)
5 comments
0
What would I want to put as code inside of the if statement? Hasn't the stats.Value been set to the saved value?
0
You don't have to put anything inside, the pcall does it already, you can print or send a message to the player if you want but that's all
0
Thank you so much for your help.
0
Np :)
0
You're creating a new function every save. Use pcall(moneyDataStore.GetAsync, moneyDataStore, scope) to avoid this.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Normally you just wrap your function inside a pcall statement, make sure to check the wiki: http://wiki.roblox.com/index.php?title=Global_namespace/Basic_functions#pcall