How do i return variables inside an if statement and that if statement is in a pcall and that pcall is in a function

How do i return variables inside an if statement and that if statement is in a pcall and that pcall is in a function, because when i return it, the script doesnt get the variables.

like this

	Function = function()
		local a
		pcall(function()	
	             --make a equal to something
                          if something = something then
				return a
                            end
			end
		end)
	end

(its in a module script)

1 Like

ill try i forgot to put if but ill try it

nope didnt work

heres the script

local module = {
	
	FindEnemy = function(TowerHumanoid,TowerTorso,Range,Type)
		local Occupied = false
		local EnemyHumanoid 
		local EnemyHMR 
		local Found = pcall(function()		
			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
		end)
		if Found and (TowerTorso.Position - EnemyHMR.Position).magnitude <= Range then return Occupied,  EnemyHumanoid, EnemyHMR end
	end
		
}

return module
local module = {}

module.FindEnemy = function(TowerHumanoid,TowerTorso,Range,Type)
	local Occupied = false
	local EnemyHumanoid 
	local EnemyHMR 
	local Found = pcall(function()		
		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
	end)
	if Found and (TowerTorso.Position - EnemyHMR.Position).magnitude <= Range then return Occupied,  EnemyHumanoid, EnemyHMR end
end

return module

This code has no return statement other than the module itself being returned at the end.

Wait, I see it now:

local module = {}

module.FindEnemy = function(TowerHumanoid,TowerTorso,Range,Type)
	local Occupied = false
	local EnemyHumanoid 
	local EnemyHMR 
	local Found = pcall(function()		
		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
	end)
	if Found and ((TowerTorso.Position - EnemyHMR.Position).magnitude <= Range) then
		return Occupied, EnemyHumanoid, EnemyHMR
	end
end

return module

It was just formatted awkwardly.

1 Like

how do i fix it when i use that it doesnt work

I didn’t change anything, I was just fixing the position of the return statement. Have you tried printing “found”? If it’s false then nothing will work (in other words if the pcall fails).

It prints but i think the other script doesnt get the variables because its in an if statement i think

i added print(returning) when script detects when its occupied and its printing but the other script isnt getting the variables

Can you show the code where you’re calling the module function? Don’t forget you’re returning a tuple of values (more than 1 value) so you’ll need to declare multiple variables to handle each value.

Is it really necessary to do a duel pcall? Not to mention you’re attempting to do this every frame.

You can do this:

Function = function()
    return select(2, pcall(function()
        if something == something then
            return a
        end
    end)))
end

select just selects the nth argument and all arguments after that. pcall returns 2 arguments, the first one is whether it successfully ran or not, the second one is either the errorMessage or the value you returned. In this case, we’re getting the value returned (the second value), hence why we used the number 2.

print(select(1, "foo", "bar", "gas")) -- foo, bar, gas
print(select(2, "foo", "bar", "gas")) -- bar, gas
print(select(3, "foo", "bar", "gas")) -- gas

local foo, bar, gas = (select(1, "foo", "bar", "gas")
local bar, gas = (select(2, "foo", "bar", "gas")
local gas = (select(3, "foo", "bar", "gas")
3 Likes