Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I use pcall properly?

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	     

1 comment

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

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.