Opcall - first resource

Hello! This is my first resource, so please make constructive criticism instead of “Hey, this resource is useless.”

What does this function do?
It’s meant to save your time, it’s basically xpcall but with the success argument, I hope you enjoy it!

function opcall(functiontorun, functiontorunonsuccess, functiontorunonerror)
	-- running function
	local success,err = pcall(function()
		functiontorun = functiontorun()
	end)

	if success then
		functiontorunonsuccess(functiontorun)
	else
		-- failed
		functiontorunonerror(err)
	end
	return success,err
end -- kinda modified by @FelixProfit.

How to use it?

local success,err = opcall(function()
--urfunctiontorun
end,
function(success,err)
--urfunctiontorunonsuccess
end,
function(success,err)
--urfunctiontorunonerror
end)

It also returns success and msg just in case you need it later, it also returns it in your given functions.

Thanks!

4 Likes

Can u tell me why u should use this compared to pcal?

Because instead of using if statements, you can directly use functions, it’s basically xpcall but with success function.

May i ask, whats xpcall? Whats the use ofmxpcall?

It’s pcall but it gives you an argument which is a function , and it runs it when it errors.

Opcall is xpcall but with the success arg, and I also believe it’s faster than xpcall as it’s using pcall.

Yoo thats so cool can u send me a code sample of it?

of xpcall or opcall?

Both pls sorry to disturb u i just want to learn it.

Oh so if you want xpcall, read this

Read the xpcall section

Tysm so xpcall is like the .try and .catch in javascript. Nice

1 Like

Thank you for appreciating my resource!

Oh yeah and for opcall read this code sample

function opcall(functiontorun, functiontorunonsuccess, functiontorunonerror)
	local result
	-- running function
	local success,err = pcall(function()
		result = functiontorun()
	end)
	
	if success then
		-- worked
		print("the result is:", result)
		functiontorunonsuccess(result)
	else
		-- failed
		functiontorunonerror(err)
	end
	return success,err
end

-- calling function
local succ, err = opcall(
	function()
		return 1 + 2 == 5
	end,
	function(result)
		print(result)
	end,
	function(err)
		print(err)
	end
)

Made it so you can return and get the result and also removed some code that isn’t needed

1 Like

cool, but ireally prefer removing print, thanks anyway

1 Like

looks nice! code is great as well. good job :slight_smile:

1 Like

UPDATE

Removed an unnessecary part in the opcall.

also i noticed that the function will be underlined red but it will still work


proof
image
thanks

It seems like someone finally made this and released it to the public, I didn’t make a post about mine :v

1 Like

Ig nice code. I really could’ve used assert lol.

Also, assert does huge memory consuming. I suggest you do the disgusting ifs.

I made a version of this but with ternary.(the fake ternary)

function opcall(functiontorun,functionerr,function success)
local success,err = pcall(function()
functiontorun = functiontorun()
end
success == true and functionsuccess(success) or functionerr(err)
end

I really like one liners, but I didn’t test it yet so I don’t think it work