Return not functioning properly?

(I am fairly new to making my own posts, so bare with me)
Hello, Currently working on a project and noticed something a little strange. Im just trying to return the function if the “dontTrigger” variable (declared earlier in the code) is true. I have a few print statements to make sure the values of things and that it’s atleast running. I want to also share that while I was testing, I replaced the “dontTrigger” variable with “true” (if true== true) and it worked, I then declared dontTrigger as true right before the if/else, and it works. However, if dontTrigger is true before hand, and is verified as true right before the if/else, the return statement won’t work. The comments inside still run, and the rest of the script still runs as well. The only thing that does not work is specifically the return statement, in specifically these circumstances. I’m not sure what to do. Here is my code if it helps:

-- Above this is code that sets dontTrigger to true.
print("Testing, dontTrigger is:",dontTrigger)
	if dontTrigger == true then
		print("AlternativeAttack | Trigger Check Working")
		return
		    print("Second trigger: Return not working?")
	end
-- Below this is what should happen if it should run, in my case it's an attack.

Here is a screenshot of the output specifically when the return does not work (no errors or warnings):

Appreciate the Help/Discussion

Return has to be the last thing you call in a scope and should be used in a function.
If I’m not mistaken this should actually throw an error but weird that it doesn’t.

Does look like a weird Luau bug but considering you use the return statement incorrect this might be the result.

Try this code.

local trigger = true
local trigger2 = false

function doStuff()
 print("Start")
 
 if trigger then

  print("Trigger1 activated")

  if not trigger2 then return end

  print("Only prints if trigger2 is also true.")
 
 else

  print("Trigger1 and trigger2 was false.")

  return
 end

 print("Will not print if trigger1 is false.")
end

doStuff()

Edit: Try the provided code.

It works, the problem is that you are returning the print() output.

More like
“Trigger1 is false, I don’t know if trigger2 is.”

1 Like

is the same as:

-- Above this is code that sets dontTrigger to true.
print("Testing, dontTrigger is:",dontTrigger)
	if dontTrigger == true then
		print("AlternativeAttack | Trigger Check Working")
		return print("Second trigger: Return not working?")
	end
-- Below this is what should happen if it should run, in my case it's an attack.

so in conclussion, your script does return, but it prints “Second trigger: Return not working?” before returning.

it’s just like:

function abc(a, b)
	return a or b
end

but a or b is changed to print("blah blah blah").

when you use return, with some parameters next to it, it will evaluate the parameters can convert it to values. in this case, you’re using print("Second trigger: Return not working?") which when evaluated will return nil, and prints out the message on your output like normal.

I see the confusion, I had a feeling this might be interpreted like this, but the problem isnt the print statement after the return. I know the statement that says “Return not working?” is only printed because the return is Technically running, the problem is that the script AFTER that still runs, rather than returning. If this was the main issue, changing the beginning parameters like I mentioned in my test cases wouldnt work I believe

in that case can i see the whole function?

Hello everyone. I am embarassed and sorry to have wasted you all’s time. After ~5 hours I realized it was infact a logic error, and the return command was running, There was simply another function that was out of scope, causing the perceived error. The importance of proofreading code 10 times over :+1:

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