Feedback on "Luau globals"

pcall. Just give an example. Really hard to figure it out how to use it, plainly relying on the documentation.

You can use pcall like this:

pcall(function()
    setTransparency(character)
end)

or like this (with parameters):

pcall(setTransparency, character)

or like this (without parameters):

pcall(functionWithoutParameters)

Would be better at least to link the official Lua documentation (Programming in Lua : 8.4) if no examples are provided.

Affected URL: Luau globals | Documentation - Roblox Creator Hub

Basically, pcalls are used like this:

success, response = pcall(f : function, ...args : any)

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)
1 Like

She is not asking on how to use it, she’s requesting for such information to be added to the documentation.

This doesn’t provide example usage, just briefly explains how it works.

Please read the second half of my post.

1 Like

Thank you!

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

1 Like