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
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.
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
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.
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.
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
@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.
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”
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
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