I need help with xpcall

I want to learn how to use xpcall

I do not know how to use xpcall

I looked on the Developer Hub and on YouTube I couldn’t get a conclusion

I am trying to learn xpcall incase one day I need it but I do not know how or when to use it. I am a beginner scripter on Lua and I just wanted to know. I need help learning what it does and when/how to use it. I only know pcall but I do not know if it is the same or not, this would be extremely useful for me and my coding life. So, if you could please try your hardest to help and have a heavenly day.

Sorry for not much detail it was hard trying to add detail in this one

xpcall works similarly to pcall, but you can provide your own error handling function, that is called with the error object as a parameter.

To my knowledge xpcall is deprecated.

so like this?:

local success, err = xpcall(function()
    print("Hi")
end)

if success then
    print("Success")
else
    warn(err)
end

No, like this.

local returned, data = xpcall(function()
	    x("This will error")
	end, function(err) -- Second function argument, with parameter that will represent the error message
	    print("Error: ", err) -- Print "Error: " before the message
	end)
	 
	print(returned, data)
local success, result_from_second_function = xpcall(function()
    print("Hi")
end,
function(err)
    print(err)
    return err
end)

So like this?:

local success, result = xpcall(function()
    print("Hi")
end,
function(err)
    warn(err)
    return err
end)

print(success, result)

It’s basically pcall expect you can pass a function to do once it fails.

For example this:

local success, result = pcall(function()
return ds:GetAsync(key)
end)

if not success then
myFunc(result)
end

Can become:

xpcall(function()
return ds:GetAsync(key)
end, myFunc)
1 Like

Why won’t you just check in studio, you have literally copied my reply

Ohhhhhhhh that is so cool thank you so much guys! This really helped and I will be posting again because there is only one more thing I searched everywhere for and never knew. lol

I am rn do not worry and I did not copy lol I typed it

xpcall is intended for situations where you want to use an error handling function, rather than just returning a boolean that tells if the call was successful or not.

Here is my example from an older thread that actually uses an error handling function:

1 Like

theres already a post
Whut is xpcall? - Help and Feedback / Scripting Support - DevForum | Roblox