:GetPartsBoundInBox() breaking while loop

When a character is found that isn’t the player, it halts the loop forever and makes it so I can not even use break.

Any help is appreciated!

Code:

while newPart ~= nil or newPart.Parent ~= nil or newPart or newPart.Parent or tickStart ~= 0.401 do
	if not newPart or not newPart.Parent or newPart == nil or newPart.Parent == nil then
		break
	end
	wait()
	local parts = game.Workspace:GetPartBoundsInBox(newPart.CFrame, Vector3.new(2,2,2), overlap)
			
	if parts then
		for _, part in pairs(parts) do
			if part.Parent:FindFirstChild("Humanoid") then
				if part.Parent:FindFirstChild("isPlayer") then return end
				if table.find(partDb, part.Parent) then return end
						
				table.insert(partDb, part.Parent)
				part.Parent.Humanoid.Health -= 10
				swordHit = true
			end
		end
	end
end

Honestly, its very hard to assess what you are doing here as you:

a) You are doing a lot of unnecessary checks

b) Didn’t give any info about what you’re trying to achieve

c) Didn’t give us any extra background info such as what were your OverlapParams

First, I would really consider revising your code, as it’s very hard for people to read, the easiest way to do this is just getting rid of all the unnecessary bluff.

while true do
	if not newPart or not newPart.Parent or tickStart == 0.401 then
		break
	end
	task.wait()
	local parts = game.Workspace:GetPartBoundsInBox(newPart.CFrame, Vector3.new(2,2,2), overlap) -- What is overlap?
	-- The for loop won't run anyways if there's nothing in it
	for _, part in pairs(parts) do
		if part.Parent:FindFirstChild("Humanoid") then
			if part.Parent:FindFirstChild("isPlayer") then return end
			if table.find(partDb, part.Parent) then return end
					
			table.insert(partDb, part.Parent)
			part.Parent.Humanoid.Health -= 10
			swordHit = true
		end
	end
end

I would assume that you’re making a hitbox system, so I would suggest giving more info.

OverlapParams:

local overlap = OverlapParams.new()
		overlap.FilterType = Enum.RaycastFilterType.Exclude
		overlap.FilterDescendantsInstances = {plr.Character, game.Workspace:FindFirstChild(plr.Name.."Fake"), newPart}
		overlap.MaxParts = math.huge
		overlap.RespectCanCollide = false

And yes, I am trying to achieve a hitbox system. The hitbox is connected to a character model that hovers above the player’s character and the character is a small sphere. I am trying to make a sword system since using tools doesn’t work for non-player characters.

Forgot to mention, the start of the code has a check for if a BoolValue is true or false. If not, the rest runs. When the while loop breaks the code, the value cannot be turned off making it impossible to run again without manually changing it to false.

i dont see anything that would yield

are your returns in the for _, part in pairs(parts) do loop meant to be continues

No, they are supposed to stop it.

Just searched up what “continue” does, and changed them to that. So now they are

I have fixed the issue, it was an issue with my startTick variable. Doing tick() - startTick >= number fixed the issue. Thank you guys for your help and time.

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