Return not working as expected

I’m trying to make a module function stop running when it meets a certain condition. Right now I have something like this (I made an example code because the original is too long):

a = 1
b = 2
c = 3

function test()
    if a < b then
      --Do some code--  

        if b > c then
             --Do some code--  
            print("Code B finished!")
        else
            --Do some code--
            print("Code C finished!")
            return
        end
        
        --Code I don't want to run if condition is met--
        print("Code D finished!")
    end
    --Code I don't want to run if condition is met--
    print("Test E")
end

test()

The problem is some of the code will run anyways even though there’s a return statement.

In this example, when it meets the condition and code C finishes, the function is supposed to stop without the rest of the code running. But when I call the function from my module from another script, the rest of the code in the if statement doesn’t run (code D) but the rest of the code outside of the if statement runs anyways (code E). I was wondering how I could fix this or if there is any reason this happens, thanks.

1 Like

When running this function with the variables you’ve supplied, the only output is “Code C finished!”, which is your expected behavior. Does this not work specifically when called from another module?

2 Likes

Yes, if I run this function in a regular script, the output is only “Code C finished!” which is what’s supposed to happen. But if I create this function as part of my module and then call that function, I would get “Code C finished!” and “Test E” which is the problem.

I’m still unable to reproduce your problem.

Supplying your example function via a ModuleScript results in the expected behavior and only prints “Code C finished!”.

This leads me to believe that perhaps there’s a problem with your original code. Is there any chance I could take a look, assuming you’re comfortable posting your work on a public forum?

1 Like
function attackModule:attack()
	if timer > cooldown then
		self:resetTimer()
		self:attackAnimation()

		if RunService:IsClient() then
			self:showAttackEffect(0)
		else
			self:showAttackEffect(1)
			return
		end
        print(Player.Name.." attacked.")
	end
	remoteEvent:FireServer("Attack")
end

Here’s a simplified version of the code that causes the same issue. The problem is the same: if I call it from the server it won’t print anything but the remote event will still try to fire and cause errors.

Sorry, I’m stumped. The following module only outputs test2 when the “attack” method is called from another script:

local attackModule = {}

local v1 = true
local v2 = false

function attackModule:attack()
	if v1 --[[timer > cooldown]] then
		--self:resetTimer()
		--self:attackAnimation()

		if v2 --[[RunService:IsClient()]] then
			--self:showAttackEffect(0)
			print("test1")
		else
			print("test2")--self:showAttackEffect(1)
			return
		end
		print("test3")--print("Player.Name.." attacked."")
	end
	print("test4")--remoteEvent:FireServer("Attack")
end

return attackModule
2 Likes