where you can have any number of arguments depending on what the function accepts.
This will return 2 values instead of the regular values a function returns: success (whether or not the function was executed successfully in the pcall), and a response (whatever the function would normally return: values or an error message).
I agree it can be a bit confusing, I made a pull request to ammend this issue here.
This is the example code I added btw:
local function divideByFive(n)
return n / 5
end
local success, response = pcall(divideByFive, "ten")
print(success, response)
This example is not the easiest one to get spot on, since it includes the failing logic. And we have to show how to handle an error in this case.
Maybe this code will be a little bit clearer, but still, I believe it’s Roblox’s team task to determine the better examples for this case.
local function divideByFive(n: number): number
return n / 5
end
local success, response = pcall(divideByFive, "notANumber") -- results in error
if success then
-- ...
else
warn("Error message: " .. response)
end
That’s a good suggestion to change the string to! I thought about including a conditional to handle the success/failure variable, but I thought it might make it longer than it needs to be to show what pcalls can do. It may be nicer to just include it anyways though