If function running both if and else statements

I have an if statement if Grabbed = false it runs it then i have the else which should run if it isn’t false but it runs anyway i’ve tried using elseif but nothings worked anyone know what might be the problem

local Grabbed = false
	if Action == "Grab" then
		if flying == true then
			for i,v in pairs(game.Players:GetChildren()) do
				local Distance = (Player.Character.Torso.Position - v.Character.Torso.Position).magnitude
				if Distance < 20 then
					if Grabbed == false then
						
						print("Grabbed = true")
						local HRP = v.Character.HumanoidRootPart
						Player.Character.Torso.CFrame = v.Character.Torso.CFrame * CFrame.new(0, 0, -1.5, 1, 0, 0, 0, 0.999992847, 0, 0, 0, 1)
						Weld = Instance.new("Weld")
						Weld.Parent = Player.Character.HumanoidRootPart
						Weld.Name = "GrabWeld"
						Weld.Part0 = Weld.Parent
						Weld.C0 = HRP.CFrame:Inverse()
						Weld.Part1 = HRP
						Weld.C1 = Player.Character.HumanoidRootPart.CFrame:inverse()
						Grabbed = true
					else 
						print("Grabbed = false")
						Grabbed = false
					end
				end
			end
		end
	end
1 Like

Did you try seeing if there might be 2 elements, and it the second one of those elements might have grabbed set as false

1 Like

What’s the value of Grabbed? Try printing that value.

1 Like

@Blockzez @Ender_BX so I tried printing the value of grabbed read the comments

	local Grabbed = false
	if Action == "Grab" then
		if flying == true then
			for i,v in pairs(game.Players:GetChildren()) do
				local Distance = (Player.Character.Torso.Position - v.Character.Torso.Position).magnitude
				if Distance < 20 then
					if Grabbed == false then
						print(Grabbed) -- grab returned as false
						print("Grabbed = true")
						local HRP = v.Character.HumanoidRootPart
						Player.Character.Torso.CFrame = v.Character.Torso.CFrame * CFrame.new(0, 0, -1.5, 1, 0, 0, 0, 0.999992847, 0, 0, 0, 1)
						Weld = Instance.new("Weld")
						Weld.Parent = Player.Character.HumanoidRootPart
						Weld.Name = "GrabWeld"
						Weld.Part0 = Weld.Parent
						Weld.C0 = HRP.CFrame:Inverse()
						Weld.Part1 = HRP
						Weld.C1 = Player.Character.HumanoidRootPart.CFrame:inverse()
						Grabbed = true
					else 
						print(Grabbed) -- grabbed is returning as true but why is this even running
						print("Grabbed = false")
						Grabbed = false
					end
				end
			end
		end
	end

It’s the for loop then, it runs even after the variable Grabbed is set to true, you can use break/goto so it won’t run after (btw, you can just use if flying then instead of if flying == true then, and if not grabbed then instead of if Grabbed == false then)

2 Likes

now it doesn’t do the elseif im so confused

	local Grabbed = false
	if Action == "Grab" then
		if flying then
			for i,v in pairs(game.Players:GetChildren()) do
				if v.Name ~= Player.Name then
					local Distance = (Player.Character.Torso.Position - v.Character.Torso.Position).magnitude
					if Distance < 20 then
						if not Grabbed then
							Grabbed = true
							print(Grabbed)
							print("Grabbed = true")
							local HRP = v.Character.HumanoidRootPart
							Player.Character.Torso.CFrame = v.Character.Torso.CFrame * CFrame.new(0, 0, -1.5, 1, 0, 0, 0, 0.999992847, 0, 0, 0, 1)
							local Weld = Instance.new("Weld")
							Weld.Parent = Player.Character.HumanoidRootPart
							Weld.Name = "GrabWeld"
							Weld.Part0 = Weld.Parent
							Weld.C0 = HRP.CFrame:Inverse()
							Weld.Part1 = HRP
							Weld.C1 = Player.Character.HumanoidRootPart.CFrame:inverse()
							break 
						elseif Grabbed then
							print(Grabbed)
							print("Grabbed = false")
							Grabbed = false
							Player.Character.HumanoidRootPart.Weld:Destroy()
							break
						end
					end
				end
			end
		end
	end

you can simply use else, since there are only 2 conditions.