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

How do I use pcall properly?

Asked by 6 years ago
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         
0
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 User#834 0 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago
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)  

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? User#21908 42 — 6y
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 User#20388 0 — 6y
0
Thank you so much for your help. User#21908 42 — 6y
0
Np :) User#20388 0 — 6y
0
You're creating a new function every save. Use pcall(moneyDataStore.GetAsync, moneyDataStore, scope) to avoid this. hiimgoodpack 2009 — 6y
Ad

Answer this question