Do You Need local Success, Error in pcalls?

I was looking at my script and it looks a bit cluttered because of all the variables the pcall is using. And I was wondering, do I need the variables before it?

local success, errorMsg = pcall(function()
--code
end)
if success then
print("Success")
else
warn(errorMsg)
end

I was wondering if I could shorten it to this:

pcall(function()
--code
end)

Using Success and errorMessage Is better, because you will know what’s wrong and if it is working or not.

If you don’t wanna use it then don’t use it…

1 Like

Well pcall returns those 2 variables, if you are using pcall without storing it then it will not detect what error you are getting pcalls are used to catch errors, you can try using xpcall, just its try {...} catch() {...} in javascript.

xpcall(function()
	print('Hello world')
    print(lets do an error) -- Error
end, function(errr)
	print('there is an error')
    error(errr)
end)

You even can get 2 variables from xpcall.

local success, data = xpcall(function()
	print('Hello world')
    print(lets do an error) -- Error
end, function(errr)
	print('there is an error')
    error(errr)
end)

if (successs) then 
   --code
end
1 Like

Yes, you can just do

pcall(function()

end)
1 Like

Yeah but it just takes up a lot of space once I’m done removing bugs.