Frustrated by failed collision detection

I’ve been trying to get this function to work for a bit now. I looked at advice for getting adjacent collision detections. I don’t want to have a region3 because I’ve heard they can be computationally expensive, so I’ve been trying the slightly larger part method of making a hitbox, but for some reason these boxes just aren’t detecting anything. The part I want to detect does have CanCollide off but has CanTouch on.

I’ve been using comically large sizes for testing purposes and it does not seem to appear at all.

-- Create a function that creates a slightly larger part that can detect adjacent parts.
local function get_touching_from_hitbox(part, size)
	local hitbox = Instance.new("Part")
	hitbox.Anchored = true
	hitbox.CanCollide = false
	hitbox.CanTouch = true
	hitbox.Size = Vector3.new(size, size, size)
	hitbox.CFrame = part.CFrame
	hitbox.Transparency = 0
	hitbox.BrickColor = BrickColor.Red()
	local connection = hitbox.Touched:Connect(function() end)
	local results = hitbox:GetTouchingParts()
	connection:Disconnect()
	if #results == 0 then
		print('Bruh')
	end
	hitbox:Destroy()
	return results
end
1 Like

You’re making a connection, and instantly disconnecting it. It probably doesn’t have enough time to detect things.

Additionally you call ‘GetTouchingParts’ only once, so if there’s no touching parts on the hitbox’s creation, you won’t get results.

I might be wrong but I think you have to parent the part to the workspace for hit detection like this to work.

1 Like