Help with simple Hitbox function

I am trying to make a Hitbox class but for some reason the hitbox part is not detecting anything with the :GetTouchingParts()
I have tried doing this witht he Default collision group. does anyone see anything that would cause this to stop working. I am just trying to detect a player/npc

function Hitbox.cast(self: HitboxClass): {BasePart}
	if self.info.shape == "Box" then
		local hitbox: BasePart = Instance.new("Part")
		
		hitbox.Parent = workspace
		hitbox.Name = "Hitbox"
		hitbox.Size = self.info.size
		hitbox.CFrame = self.attatchedTo.CFrame * CFrame.new(self.info.offset)
		hitbox.Orientation = self.attatchedTo.Orientation + self.info.rotation
		hitbox.CanCollide = false
		hitbox.CanQuery = true
		hitbox.CanTouch = true
		hitbox.Anchored = true
		hitbox.CollisionGroup = self.info.collisionGroup
		
		if DEBUG then
			hitbox.Transparency = 0.9
			hitbox.Color = Color3.new(1, 0, 0)
			hitbox.Material = Enum.Material.Neon
		else
			hitbox.Transparency = 1
		end
		
		local parts = hitbox:GetTouchingParts()
		print(hitbox)
		print(parts)
		print(hitbox.CollisionGroup)
		if not DEBUG then hitbox:Destroy() end
		
		return parts
	end
end

GetTouchingParts only works if CanCollide is true, unless you add a TouchInterest. Otherwise it returns an empty list, like you’re seeing.

You can use WorldRoot:GetPartsInPart() instead to avoid the TouchInterest work around.

1 Like

so do both colliding parts need to have CanCollide set to true then?

If the hitbox has no collisions or touch interest, yes.

Or you can just add something like:

local touchInterest = Instance.new("TouchInterest")
touchInterest.Parent = hitbox

Or you can switch GetTouchingParts for Workspace:GetPartsInPart(hitbox), which works for CanCollide = false.

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