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

Whats the difference between doing then and pcall?

game.Players.PlayerAdded:Connect(function(plr)
local S,E = pcall(function()
	local GotAsync = DataStore:GetAsync(plr.UserId)
end)

if S then


game.Players.PlayerAdded:Connect(function(plr)
	local GotAsync = DataStore:GetAsync(plr.UserId)

if GotAsync then

2 answers

✓ Accepted answer

pcall()

Basically wraps a block of your script, that then when ran, it will catch any errors that might occur, then returning it’s status - which will be a boolean, there are three major common ways that people use pcall,

Method 1:

— If pcall returns true, then it ran successfully, else it returns false, and note that it didn’t run correctly

if pcall(<somefunction>) then
— SUCCESS
else
— ERROR
end

Method 2:

local Status = pcall(<somefunction>)

if Status then
— SUCCESS
else
— ERROR
end

Method 3:

— This time, pcall will edit one of your designated variables to be true, the 1st variable designated - and if it’s true, it means, it ran successfully, else it makes the second designated variable to be true, which means, a error

local suc,err= pcall(<somefunction>)

if suc then
— SUCCESS
elseif err then
— ERROR
end

I don't exactly understand the question but what this is doing here is trying to get data from a data store. If the data is not found it would produce an error. Since it is inside a pcall function though, errors won't be produced and any errors that do occur will break out of the pcall function. S and E are returned. S for success and e for error. If there was an error, error contains a string of the error.

This discussion is preserved as it was. New answers, comments, and votes are closed.