Using return to stop a function from within another function?

Basically I have the main function and I wanted the main function to return something inside the second function OR NOT do anything at all depend on the condition. Heres an example

local MainFunction = function()
  local Secondary = function()
    if conditions == true then
      return?? (I need this to return something on the main function not secondary)
    end
  end

  Secondary() --if possible I want to keep the thing need to return the MainFunction as short as possible

end
2 Likes

You could possibly try making the Secondary function return true, and make a condition in the main function that does a return when the secondary function returns true. Something like this:

local MainFunction = function()
	local Secondary = function()
		if conditions == true then
			return true
		end
	end
	
	if Secondary() then
		return
	end
end
1 Like

Yeah I understand that but the problem is that I need to do this multiple times and it would be hella messy if I have to put that everywhere hence why the Secondary function is there in the first place, to reuse code blocks

It’s possible. Please specify your specific use case

Can we get some example code of where you are trying to use this?

1 Like

I want to make a special function that will be used in every code (basically a function in a module) and that said function main objective is to check the condition and if the condition is true then it will stop the whatever function it was used in using a return. This way I won’t have to put if then statements everywhere checking the condition

For something as such, I would use a top level module as you specified. Stacked localised functions aren’t recommended. I only use them for localised overloading.

Can you give another summarised example code? I dont think I understood your intention quite well.

local ActionCanceled = false
local DoAction = function()
  do something
  wait(1)
  do more things
  wait(3)
  even more things
  wait(2)
  action finished so clean up whatever Instance is created (aka doing more things)
end

I want that when the ActionCanceled is set to true it stop the DoAction function entirely, if I were to use the normal if then method I would need to put it after every single wait

I would just use multiple guard clauses

if CheckCondition(ThingToCheck) then
	return
end

or

if CheckCondition(ThingToCheck) then return end

So you would have

local ActionCancelled = false

local DoAction = function()
	if ActionCancelled then return end
	task.wait(1)
	if ActionCancelled then return end
	task.wait(2)
	if ActionCancelled then return end
	task.wait(3)
end
1 Like

This seems like the prime use-case to use something like Promises. Its a very popular resource that allows you to cancel tasks that would otherwise complete in the future.

Otherwise your other option is using coroutines.
Then when your criteria is met you cancel the thread coroutine.cancel

You can learn about Promises here:
https://eryn.io/roblox-lua-promise/

I can provide example code aswell

2 Likes

You should use the task library and kill the task on detecting the change.


local function things()
end

local thread = task.spawn(function()
    things()
    task.wait(5)
    things()
end)

-- detection logic
boolValue.Changed:Connect(function()
    task.cancel(thread)
end)
2 Likes

could you provide me with a code example of promise? I read the thing but dont quite understand

1 Like
Promise = require(somewhere.PromiseModule)

local CurrentAction

function DoAction()
	CurrentAction = Promise.new(function(resolve)
		--do something
  		wait(1)
  		--do more things
  		wait(3)
  		--even more things
  		wait(2)
  		
		resolve() --This marks the task as finished!
	end)
end

function CancelAction()
	CurrentAction:cancel()
end

WhenSomethingHappens:Connect(function()
	CancelAction()
end)
1 Like

What’s cool about Promise is that it can also allow you to do this!

In the scenario where it gets canceled before cleaning up, you can do:

Promise = require(somewhere.PromiseModule)

local CurrentAction

function DoAction()
	CurrentAction = Promise.new(function(resolve)
		--do something
  		wait(1)
  		--do more things
  		wait(3)
  		--even more things
  		wait(2)
  		
		resolve() --This marks the task as finished!
	end)

	CurrentAction:finally(function() --will run after the action, despite canceling
		clean()
		DestroyInstances()
	end)
end

function CancelAction()
	CurrentAction:cancel()
end

WhenSomethingHappens:Connect(function()
	CancelAction()
end)
1 Like

This module seem really good, thanks for introducing me to it, Ill try it out later

1 Like

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