What is pcall? What does it do?

Hello! SO I’m trying to learn scripting, What is Pcall and what does it do, How does it work, and what can it be used for?

A pcall will run a set of instructions, usually functions, and returns a tuple (a table/ list) containing whether the code ran errored or not, and any of the returned values if it were to have succeeded. If it didn’t succeed, however, it would return the error message. Developers tend to use pcall functions for uses such as getting/ saving data.

2 Likes
local success, errorMsg = pcall(function()
	local test = 1 + "A tring"
end)

print(success, errorMsg)

Output:
false attempt to perform arithmetic (add) on number and string

local success, out = pcall(function()
	local test = 2 + 10
	return test
end)

print(success, out)

Output:
true 12

2 Likes

I’m leaving this here as your information alone is not sufficient enough.
What it is - https://devforum.roblox.com/t/pcall-function/1264795
When and how - https://devforum.roblox.com/t/pcalls-when-and-how-to-use-them/393687

1 Like