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

HELP WITH pcall? [HELP]

Hi, I wonder what pcall does, and can someone explain it to me. I plan on using it in Data Stores. Thanks!

1 answer

✓ Accepted answer

###Normally, when your script throws an error, it would print an error message and then break the script, so any code written after the error would not run. However, with pcall (which stands for protected call), the output errors don't show, and if the script does throw an error, it will not break. Take this for example:

local success, message = pcall(function()
    -- pcall returns two things, a boolean if it didn't throw an error, and if it DID, returns the error msg

    PRiNt("HI") -- attempt to call a nil value 
end)

if success then -- if it didn't throw an error
    print("No errors! :D")
else
    -- there was an error :(
    print(message) --> attempt to call a nil value 
end

###You decide whether or not to print the error message. The script will continue execution even if it threw an error.

1 comment

0

Wow thanks!

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