Whut is xpcall?

Your post is highly unprofessional and can be considered inappropriate on the forum. Please use correct grammar and behave more maturely.

2 Likes

Pcall accepts one function and returns if it worked and whatever the code returned if it did or an error message if it didn’t work.
Xpcall accepts two functions, the first one is the same use as pcall but the second one is what is called when the function errors, used as error handling.

Xpcall is almost the same as pcall, it is used for running functions but doesn’t break your script if the function errors.

Crazy part is I was going to ask a question on what xpcall is LOL

1 Like

Xpcall is like pcall but you can set your own error message for testing

It’s basically just pcall but you can give a custom error handler function. It doesn’t have that many usecases if I had to assume, but for this case, I’ll make one up

Say you have a set of guis that handle simple addition depending on what the player gives, you could do it like this

local function addNumbers(x,y)
	return x + y
end

Button.MouseButton1Click:Connect(function()
	local num1 = tonumber(Input1.Text)
	local num2 = tonumber(Input2.Text)
	print(addNumbers(num1,num2))
end)

And it would work fine, but if you give it something that isn’t a number, it’ll try to add a number with nil, you could do

local function addNumbers(x,y)
	return x + y
end

Button.MouseButton1Click:Connect(function()
	local num1 = tonumber(Input1.Text)
	local num2 = tonumber(Input2.Text)
	if not num1 or not num2 then
		print("One of then numbers is not a full number!")
		return
	end
	print(addNumbers(num1,num2))
end)

And it prevents it from trying to add nil, but, since this relies on an error to happen, a custom error handler can be used, hence xpcall could be used rather than if statements

local function addNumbers(x,y)
	return x + y
end

local function errorHandler(err)
	return 0 
end

Button.MouseButton1Click:Connect(function()
	local num1 = tonumber(Input1.Text)
	local num2 = tonumber(Input2.Text)
	
	local success, result = xpcall(addNumbers, errorHandler, num1, num2)
	
	print(result)
end)

Let’s assume you give it “bob” and 10, the code would do this

Convert both of them to numbers, one of them is a string, so it gets converted to nil, then xpcall is ran.

Uh oh we’re trying to add a number with nil, time to run the given error handling function and return 0!

So if we use the bob and 10 example again, it will return 0 because the function we gave it returns 0 if an error happens. This saves having to do if statements or similar if you plan on making it do that functionality. And it’s not restricted to returning numbers, it can return anything

You can even do this

local function errorHandler(err)
	return "Hey mate you didn't give 2 numbers"
end

And if it errors, it’ll return Hey mate you didn't give 2 numbers

This is probably an example that almost no one would use xpcall for, but I had no other idea

I may’ve also worded it terribly so please tell me if oyu’re confused

6 Likes
xpcall(function()
  print(game.Players.JayO_X:CheckIsAmazing())-- true
  print(game.Players.Retro_Jono:CheckIsAmazing())-- true
  print(game.Players.MistahSplootch:CheckIsAmazing())-- true
  print(game.Players.TheUnderratedDev:CheckIsAmazing())-- true
  print(game.Players.EmbatTheHybrid-- true
  print(game.Players.ZINTICK:CheckIsAmazing())-- false
-- if some one isn't amazing we do bellow tell me if i am correct
end, 
function()
   print(game.Players.JayO_X:CheckIsSmart())-- true
  print(game.Players.Retro_Jono:CheckIsSmart())-- true
  print(game.Players.MistahSplootch:CheckIsSmart())-- true
  print(game.Players.TheUnderratedDev:CheckIsSmart())-- true
  print(game.Players.EmbatTheHybrid:CheckIsSmart())-- true
  print(game.Players.ZINTICK:CheckIsSmart())-- false
end)
3 Likes

Look it up on the DevHub (https://developer.roblox.com).

1 Like

i didnt understand there definition after reading it 35 times so thats why i asked

xpcall is used for handling errors and pcall is used to check if something has ran without any errors

3 Likes

Xpcall and Pcall are both error handlers and are both used to check if something ran fine or had an at some point during the function, the difference is that xpcall can be given a custom function for handling the given error

2 Likes

Try looking up the things you don’t understand, hope this helps! :smile:

In short, pcall runs code and if that code doesn’t work, instead of breaking the script it tells you why it didn’t work

Xpcall is basically the same but you can also tell it what to say under some circumstances

1 Like

i did check and read the the lua globals but didnt get it

1 Like

Tell me if you don’t understand this.

--Pcall function stands for protected call.

local Success, ErrorMessage = pcall(function()
	print(workspace.Part)
end)

if not Success then 
	print(ErrorMessage)
else
	print("Success")
end

--Xpcall function

local Success, Part = xpcall(function()
	print(workspace.Part)
end, function(Error) --If the code above errors
	print(Error)
end)

if not Success then
	print(Part)
else
	print("Success!")
end

In my own word pcall standing for protected pcall is to check if something ran successfully. The only way you can handle errors using pcall is by using the 2nd argument.

 local Success, Error = pcall(function()) 
 Success returns a Boolean if code ran successfully and Error is a variable that holds the Error.

I currently dont know the differences between doing 

if error then 
   print("Error", error)
end 

Versus an xpcall call. But thats all I can say hope it helped.
2 Likes

thx for the support
@EmbatTheHybrid
@JayO_X
@MixedConscience
@Retro_Jono
@lookLOLtrelakor
all of you helped me understand and other devs who see this post

4 Likes

i dont think the // are neccsesarty

You don’t need to check if an error had happened with that if statement, the given error handler function only runs if the function errored, if it didn’t, then the function never runs.

An e rror will always be sent to the error handler function so the if statement will always pass

2 Likes

I know I learned xpcalls like 5 mins ago.

2 Likes

And also, if the error handler function doesn’t return anything, Part, the 2nd return of xpcall will be nil, so I think return Error would be better in that case than print(Error) so if it wasn’t successful, then it will print out the error there, but it’s preference if you want to print hte error in the function or as the 2nd return unless you’d be needing to use it

Although I think it was a bit unneeded of me to mention that bit, sorry aobut that!

@ZINTICK My hands are really good at typing fast at the cost of accuracy lol

1 Like