Ad
Log in to vote
0

Is there a wiki page for pcall(function()?

Asked by 2 years ago

I like to read random code in my free time and try to learn new things. Is there a wiki page for this line:

pcall(function()

If there isnt a wiki page could somebody give me a rundown of this line :)

2 answers

Log in to vote
2
Answered by 2 years ago

There is a wiki here

But I'll still explain (like I always do)

pcall is to catch errors, wich means that if your script would error, pcall will tell you that error, and just continue with the script.

This is very handy for things like datastore and remote functions.

You can make a pcall function like this

function apple()
    print('apple')
end

local success, message = pcall(apple) -- DO NOT ADD '()' AFTER THE FUNCTION!!

if success then
    -- Your code worked!
else
    print('Your code didn\'t work, error:'..message)
end

If you did for example this

function apple()
    prit('apple')
end

local success, message = pcall(apple) -- DO NOT ADD '()' AFTER THE FUNCTION!!

if success then
    -- Your code worked!
else
    print('Your code didn\'t work, error:'..message)
end

It will continue the script and will print you the error (if you want, you can use warn() so you can see it easier)

The other way is this

local success, message = pcall(function()
    print('Hey')
end)

And you can do the same with the success, and message variables it returned

Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

https://wiki.roblox.com/index.php?title=Global_namespace/Basic_functions#pcall

pcall is used for catching errors when functions are passed. Helps with game errors etc.

Answer this question