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

HELP WITH pcall? [HELP]

Asked by
Oficcer_F 207 Moderation Voter
5 years ago
Edited 5 years ago

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

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

0
Wow thanks! Oficcer_F 207 — 5y
Ad

Answer this question