How to kill function by calling argument

I’m making a module so that you can loop without having to use continue as thats only a luau thing. Right now the module works like so:

Loop:Loop(1, 10, 1, function(index, kill)
	if index == 5 then return end
	if index == 7 then kill() return end
	
	print(index)
end)

Loop:Loop({"apple", "banan", "orange"}, function(index, value, kill)
	if index == 2 then
		kill()
		return
	end
	
	print(index, value)
end)

As you can see right now for the loop to end after you use kill you need to call return which is annoying to do. To continue the loop just use return and using kill will stop the function from running. Here is the module code:

local arguments = {...}
	
	local _break = false
	if typeof(arguments[1]) == "number" then
		for index = arguments[1], arguments[2], arguments[3] do
			if _break then break end
			
			arguments[4](index, function()
				_break = true
			end)
		end
	elseif typeof(arguments[1]) == "table" then
		for key, value in next, arguments[1] do
			if _break then break end

			arguments[2](key, value, function()
				_break = true
			end)
		end
	end

All this dose is call the function and pass in the values such as index, key, value etc… It also passes in the kill function and when called it sets _break to true so that next loop will break. However this will not kill the function so I also need to call return afterwards. I have already tried using threads and killing the thread once _continue is set to true however I was trying to task.cancel() the thread and it was nil. If you know how to do this please lmk!

Using break for loops.thats a way to kill an argument

Or use task.cancel

Please read before responding. I have already tried using task.cancel() on the kill function and the task variable is nil.

Your one,u should use break(this is the best option)(Just put a condition in there and u are good to go

And also this script is confusing

I’m not sure what you mean, could you explain. When _break is set to true next loop the loop will break (as expected). What I’m look to do is stop execution when _break is set to true on the callback function

This is so silly,u just need to do this

If _break == true then break end

That wont work. but it’s alright I found out how. All I needed to do was call error() and wrap it in a xpcall that way I could do error handling even when i call error() without throwing an auctual error

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.