Problem with detecting things using a if statement

Im trying to detect if a block is floating in the middle of the air and im using
GetPartBoundsInBox

 local IsBlock = false

	--pcall(function()
		for i, HitParts in pairs(hitContents) do
			if HitParts.Name == "Baseplate" then
				IsBlock = true
				--print(HitParts)

			else
				if HitParts.Name ~= "Block" or  HitParts.Name ~= "HitBox" or HitParts.Name ~= "Invisi" then
					print(hitContents)
					IsBlock = false
					--print(HitParts)
					--print(SnapHRP)
					
				else
					IsBlock = true
					print(hitContents)
					break
				end

			end
			--IsBlock = false
		end
	--end)
	--print(IsBlock)
	--wait(0.01)
	if IsBlock == false then
		
		Block:Destroy()
		print("Not touching Eachother")
	end

	IsBlock = false


It returns This
                [2] = Block,
                [3] = Right Leg,
                [4] = Left Leg,
                [5] = Left Arm,
                [6] = HumanoidRootPart,
                [7] = Head,
                [8] = Torso,
                [9] = Handle,
                [10] = Right Arm,
                [11] = InnerHitbox,
                [12] = HitBox,
                [13] = HitBox
as you can see it say HitBox So it should be true but it isnt

You’re looping through everything even after it gets set to true, meaning it can be overwritten.
Use this instead:

for i, HitParts in pairs(hitContents) do
	if not IsBlock then
		if HitParts.Name == "Baseplate" then
			IsBlock = true
			--print(HitParts)

		else
			if HitParts.Name ~= "Block" or  HitParts.Name ~= "HitBox" or HitParts.Name ~= "Invisi" then
				print(hitContents)
				IsBlock = false
				--print(HitParts)
				--print(SnapHRP)

			else
				IsBlock = true
				print(hitContents)
				break
			end

		end
	end
	--IsBlock = false
end

Let me know if that works.

2 Likes

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