How do i return a variable inside a function that is inside an if statement

how do i return a variable inside a function that is inside an if statement in a module script and get the returned variable
like this

local module = {

 a = function()
    local Go
     Go = true --this is an example
    if Go == true then
           return Go --Grab this variable from another script
    end
 end

}

return module
1 Like

Why are you using a single “=” in if statement?
Plus you already did that.

local var = module.a()
print(var) --expecteded output: nil (go has not been returned as it's a nil)

that doesnt work when i return something from an if statement the other script doesnt detect it
i tried it a while ago

As easy as:

local module  = require(modulelocation)

local var = module.a()

Althought might be easier to return Go regardless of value so that the calling script knows if it’s false.

return, returns something, here is a code example

local function getGo_Value(go)
	go = true
	if go == true then
		return go
	end
end

local go = getGo(Boolean Value here)

Also you are already setting go to true, so there is no need to add in an if statement

Try rewriting the code.

local module = {
 a = function(Number)
  if (Number > 20) then
      return "Number is bigger than 20"
else
    return "Number is smaller than 20" 
end
end
}

return module

its printing nil because its in if

its an example like doing something then setting that to true

It will return nil, as Go has no any variable assignment to it.

You need to assign GO as a param in the getGo() function, and then assign it to a variable using local whateverVariableName = getGo(The bool value go here)

can u give me an example script?
heres my script

local module = {
	
	FindEnemy = function(TowerHumanoid,TowerTorso,Range,Type)
		local Occupied = false
		local EnemyHumanoid 
		local EnemyHMR 		
		
		for i,v in pairs(game.Workspace.Rigs.Zombies:GetChildren()) do
			EnemyHumanoid = v:FindFirstChild("Humanoid")
			EnemyHMR = v:FindFirstChild("HumanoidRootPart")
			if EnemyHumanoid then
				if Type == "Auto" and (TowerTorso.Position - EnemyHMR.Position).magnitude <= Range then
					print("FoundEnemy")
					if not Occupied then
						Occupied = true
						EnemyHMR.DescendantRemoving:Connect(function()
							Occupied = false
						end)
					else 
						break
					end
				elseif Type == "SemiAuto" and (TowerTorso.Position - EnemyHMR.Position).magnitude <= Range then
					print("FoundEnemy")		
				end
			elseif (TowerTorso.Position - EnemyHMR.Position).magnitude > Range then
				Occupied = false
			end			
		end
			
		if Occupied then 
			print("Returning")
			return Occupied,  EnemyHumanoid, EnemyHMR 
		end
	end
}

return module

other script

while wait() do	
	pcall(function()
		local Occupied, EnemyHumanoid, EnemyHMR = FindTarget.FindEnemy()
		FindTarget.FindEnemy(TowerHumanoid,TowerTorso,Range,"Auto")
		print(FindTarget.FindEnemy())
		if EnemyHumanoid then
			PlasmaRay.Transparency = 0.1
			Dist = (TowerTorso.Position - EnemyHMR.Position).magnitude
			PlasmaRay.CFrame = CFrame.new(script.Parent:WaitForChild("PlasmaGun"):WaitForChild("Spinner"):WaitForChild("Start").WorldPosition,EnemyHMR.Position)*CFrame.new(0,0,-Dist/2)
			PlasmaRay.Size = Vector3.new(0.1,0.1,Dist)
			TowerHMR.CFrame = CFrame.lookAt(TowerHMR.Position,Vector3.new(EnemyHMR.Position.X,TowerHMR.Position.Y,EnemyHMR.Position.Z)) * CFrame.Angles(0,0,0)
			PlayStance:Stop()
			PlayAttack:Play()
			EnemyHumanoid:TakeDamage(20)
			script.Parent:WaitForChild("DamageDealt").Value += 1
			game.ServerStorage.GiveMoney:Fire(1)
			script.Parent:WaitForChild("PlasmaGun"):WaitForChild("PlasmaBall").Transparency = 0.8
		elseif Occupied == false then
			PlasmaRay.Transparency = 1
			script.Parent:WaitForChild("PlasmaGun"):WaitForChild("PlasmaBall").Transparency = 1
			PlayAttack:Stop()
			PlayStance:Play()
		end
		
		TowerTorso:WaitForChild("Info"):WaitForChild("Frame"):WaitForChild("Top"):WaitForChild("TotalDamage").Text = "DMG: "..script.Parent:WaitForChild("DamageDealt").Value
		TowerTorso:WaitForChild("Info"):WaitForChild("Frame"):WaitForChild("Top"):WaitForChild("TowerName").Text =  script.Parent:WaitForChild("TowerName").Value
		TowerTorso:WaitForChild("Info"):WaitForChild("Frame"):WaitForChild("Bottom"):WaitForChild("Owner").Text =  script.Parent:WaitForChild("Owner").Value
	end)
end

Try printing “Occupied” variable
Plus none of your if gates turn Occupied into a boolean with “true” - Nevermind

when i do it it keeps printing --nil

it makes it true read it says if occupied = false then occupied = true

try printing something in this line of code

EnemyHMR.DescendantRemoving:Connect(function()
							Occupied = false
						end)


thats for when the target gets destroyed

print(module.FindEnemy(Obj,Obj,Numb,Type))

try doing that.

If that won’t work, return data in a table

Ok i know why is that a nil, but why did you do that?

print(FindTarget.FindEnemy())

Remove the pcall too

im trying to print the return values

No, that’s not how it works.

print(Occupied,EnemyHumanoid,EnemyHMR)

instead of this