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

Can someone explain pcall function please?

Asked by 4 years ago

All I know is that if it gets an error it will not crash and the error will be stored and can be called.

3 answers

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

Sidenote

I already see some good answers on this thread but I feel like they neglected something.

Question

Can someone explain pcall function please?

Answer

The pcall function is an error handling function. You pass a function through pcall and it will return a boolean and a response which may be a tuple.

The boolean is whether the function successfully ran without erroring and the response is the information returned by the function or the error message that was generated.

The tuple will be a variadic set of arguments returned. For example, consider the function Workspace:FindPartOnRay which returns more than one variable such as the part it hit and where it hit.

You can also pass extra arguments besides the function which will be passed to the function when its called.

Example 1:

In this example, the function returns a tuple "a", "b", "c" and will successfully run.

local success, arg1, arg2, arg3 = pcall(function()
    return "a", "b", "c"
end)

print(success, arg1, arg2, arg3)

Which should output true a b c

Example 2:

In this example, the function will have extra arguments passed to it.

local success, response = pcall(function(arg1, arg2, arg3, arg4)
    print(arg1, arg2, arg3, arg4)
end, 1, 2, 3, 4)

print(success, response)

Which should output true nil 1 2 3 4 (nil because the function didn't return anything)

Exampe 3:

In this example, the function will error.

local a = nil

local success, response = pcall(function()
    a()
end)

Which should output false attempt to call upvalue 'a' (a nil value) because the function did not successfully run because it errored and it errored because our upvalue a was a nil value and you can't call nil values.

Further Reading

https://www.lua.org/pil/8.4.html

https://pgl.yoyo.org/luai/i/pcall

Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

pcall runs a given function and instead of ending the thread and erroring, it will stop and return whether or not the given function ran without any errors and if there is an error it will also return the error message, that you would typically see in the ouput if something errored outside of a pcall. Heres pcall in action in an error-free function:

local funcFinished,msg = pcall(function()  --pcall an error-free function
    print("boopity boop, safe function")
end)
print(funcFinished,msg)
--outputs -> "true,nil" because the function completed with out any errors

Heres a pcall with an unsafe function:

local funcFinished,msg = pcall(function()  
    () -- this will error
end)
print(funcFinished,msg)
--outputs -> "false,unexpected symbol near ')'" because the function errorred and the error message describes the error.

Generally, you would use pcall when invoking functions that you can't control whether or not they error. I mostly use this for calling sketchy roblox api functions such as using datastores or having the server request remote functions on the client, because you never know if the client will error.

Also, if you're feeling lazy and you are experiencing an error that only happens occasionally and doesn't break anything and you can't figure out why, you can be a bad programmer and just wrap it in a pcall.

Log in to vote
1
Answered by
sheepposu 561 Moderation Voter
4 years ago
Edited 4 years ago

Yeah you pretty much know all of it. It returns two values, Success and error. Success is a bool value to know whether or not there was an error. error, as you know, returns the error that occurred. Here's an example of using it in saving

local ds = game:GetService("DataStoreService"):GetDataStore('DataStore')

game.Players.PlayerRemoving:Connect(function(plr)
    local id = plr.UserId

    local saves = {
        ['Coins'] = plr.Coins.Value
        ['Level'] = plr.Level.Value
    }

    local success, err = pcall(function()
        ds:SetAsync(id, saves)
    end)

    if success then
        print('Successfully saved data')
    else
        error('Error...\n'..err)
end)

Answer this question