Can you return through multiple functions?

Let’s say you have code like this. This isn’t my exact code but hopefully this is a good example

local function check()
    if condition == true then
        return true
    end
end

local function loop()
    for i = 30, 0, -1 do
        check()
        wait(1)
    end
end

Is there some way to make the check function return through the loop function? Basically, my goal is that if the check function returns true, then the loop function would also return true and stop without using an if statement. I was thinking maybe the check function could somehow tell the loop function to return true. Idk if this is possible

2 Likes

This will return errors, if you return true then it will be

for i = 30, 0, -1 do
        true
        wait(1)
    end

You cannot just type true in a line, you have to do something like:

for i = 30, 0, -1 do
        if check(i) == true then
     end
end
1 Like

The value a function returns is discarded if it’s not saved to a variable, so this isn’t accurate.

function true 1

function true 2

2 Likes

I don’t believe it’s possible. You’d have to do something like this with if statements:

local function loop()
    for i = 30, 0, -1 do
        if check() then
            return
        end
        
        wait(1)
    end
end

Maybe we can help more if you send us the code that’s causing problems.

1 Like

This is accurate you cannot simply run true,true is not a function its a boolean

1 Like

But it’s not running true in the first place…? The function is returning a boolean, but that doesn’t mean the boolean is the thing being run. Look at the example I gave, as you can see, there literally weren’t any errors. You should test it for yourself.

1 Like

The example you gave does not return a boolean, its just prints the value

In his script, he is running a boolean, thats totally an error

1 Like

Where is he “running a boolean”?? I even tested his own code myself, it DID NOT error. Can you please explain what you mean?

local function check()
	if true then
		return true
	end
end

local function loop()
	wait(1)
	if check() then
		return true
	end
end

for i = 1, 30 do
	print(loop())
end
1 Like

It’ll just ignore the return if it’s not used. It’s rare it would break the code if it’s returned from a function.
It has always worked like this & it’s intentional behavior. I have no idea what you are on about.

1 Like

There are legit reasons you would want the same function to be usable both in an if statement & without an if statement. That is intentional & is not code-breaking behavior. If a return is not used it’s ignored & it has actual use cases.

1 Like

Nope, it actually breaks the code

What @Forummer did was correct, you cannot just do a function if its true without any if or elseif statement

return check() or loop()

local function check()
    if condition == true then
        return true
    end
end

local function loop(override)
	local start = override or 30
    for i = start , 0, -1 do
        wait(1)
        return check() or loop(i - 1)
    end
end
1 Like

@Judgy_Oreo even legit showed you that he ran it & it didn’t error. Have you even considered trying it? It isn’t breaking & has use cases. It’s not the op’s intended goal which @Forummer gave a solution to, but that’s completely besides your point of this being code breaking. It does not run “true”, it completely ignores it & true is never ran as a line on it’s own. It automatically ignores it & it’s intended behaviour. Anything else is misinformation on how return works.

2 Likes

If you ever understood my words you wouldn’t argue

See

local function random()
    return true
end

for i = 1,50 do
   random()
   wait(1)
end

This is what @specialwhat did
It wouldn’t work as this is what its actually like

for i = 1,50 do
   true -- random(), what you mean by just typing a true
   wait(1)
end

This is how you actually do it:

for i = 1,50 do
   if random() == true then -- waits if its true
    wait(1)
  end
end

You don’t simply type like this:

true
wait(1)
false
wait(1)
true
false
true
true
true

It makes no sense, I assume this is how you do it

@Deadwoodx functions DO NOT behave like this! I highly suggest you run the code yourself.

What he is saying is that, returning a value from a function doesn’t inject it into the code.
It simply gets casted into the void since you’re not assigning it to a variable.

function func()
   return 1
end

local value = func() -- assigns 1 to “value”
func() -- casts 1 to the void since it’s not assigned 

print(“complete”, value) -- outputs “complete 1”

1 Like

Very useful for loop. Let’s just replace it with an if check.

local function check()
    if condition == true then
        return true
    end
end

local function loop(override)
    local thing = override or 30
    if thing ~= 0 then
        wait(1)
        return check() or loop(thing - 1)
    end
end
1 Like

the for loop makes sure the loop times out after 30 iterations per OP’s post

Here’s an example where you could run a function without using the return, but also have the return availble for when you want to use it. It is intended behaviour for the return, to ignore & not break if not used.

local Balloons = workspace.Folder
local MaxBalloons = 4
function SpawnBalloon()
	if #Balloons:GetChildren() < MaxBalloons then
		game.ServerStorage.Balloon:Clone().Parent = Balloons
		return true
	end
	return false
end

for i=1, MaxBalloons do -- Setup balloons
	SpawnBalloon() --This won't run true or false, return is ignored. No errors.
end

while task.wait() do -- Spawn new balloons if Max is increased or balloons removed
	if SpawnBalloon() then -- But you can still use the return when you need to
		print('Balloon Popped!') -- Do something else when new balloon spawned
	end
end
2 Likes