Help with Hitboxes

So, for a tower defense game, I would like to code hitboxes so towers can be placed too close to each other. But when I call the function to detect touching coming from other parts, it doesn’t run UNTIL I’ve finished placing my tower when it should be doing it on the time of placement.

UPDATE: It was hitting another part WITHIN the hitbox, causing it to run. But I fixed that, now the issue is that it’s only touching the player, and nothing else.

local function AddPlaceholderTower(name)
	local towerExists = towers:FindFirstChild(name)
	if towerExists then
		RemovePlaceholderTower()
		towerToSpawn = towerExists:Clone()
		towerToSpawn.Parent = workspace.Towers

		local hitbox = game.ReplicatedStorage.Hitbox:Clone()
		hitbox.Parent = workspace
		hitbox.Position = towerToSpawn.HumanoidRootPart.Position - Vector3.new(0, 1.25, 0)
		hitbox.Size = getTowerHitboxSize(towerToSpawn.Name)
		hitbox.CanCollide = false
		hitbox.CanQuery = true
		hitbox.CanTouch = true
		placementHitbox = hitbox
		local weld = Instance.new("WeldConstraint")
		weld.Parent = hitbox
		weld.Part0 = towerToSpawn.HumanoidRootPart
		weld.Part1 = hitbox

--the touch scripts
		placementHitbox.Touched:Connect(function(hit)
			if (hit.Parent.Name == "Hitboxes" or hit.Parent.Name == "Path") and hit.Parent ~= placementHitbox and canPlace == true then
				print("WHY MUST I BE VICTIM OF SUCH MISFORTUNE i cannot place")
				canPlace = false
				colorPlaceholderTower()
			end
		end)
		placementHitbox.TouchEnded:Connect(function(hit)
			if (hit.Parent.Name == "Hitboxes" or hit.Parent.Name == "Path") and hit.Parent ~= placementHitbox and canPlace == false then
				print("WHY MUST I BE VICTIM OF SUCH MISFORTUNE i can place")
				canPlace = true
				colorPlaceholderTower()
			end
		end)

		CreateRangeCircle(towerToSpawn, true)

		for i, object in ipairs(towerToSpawn:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Tower"
			end
		end
	end
end


P.S. It is supposed to go red once the HITBOX collides with the path, not when the tower is ON the path.

1 Like

Don’t use TouchedEvents for this! Instead… you, can use workspace:GetPartsBoundsInBox()

Example of Usage

local HitboxPart = hitboxparthereblabla
local PlaceholderModel = previewthingymodel
local TowerParent = towerworkspace -- where the towers is placed under (tower.Parent)

local OverlapParameters = OverlapParams.new()
OverlapParameters.FilterType = Enum.RaycastFilterType.Include
OverlapParameters.FilterDescendantsInstances = {TowerParent}

local Check = workspace:GetPartsBoundsInBox(HitboxPart.CFrame, HitboxPart.Size, OverlapParameters)
local OverlappingTowers = 0

if #Check > 0 then
for i,OtherTower in Check do
if not OtherTower:IsDescendantOf(PlaceholderModel) then
OverlappingTowers += 1
break
end
end
end

if OverlappingTowers > 0 then
print("overlapping!") -- overlapping with other tower
else
print("mmm quite a comfy spot indeed") -- clear
end
1 Like

As FN Herstal said, .Touched is infamously unreliable. Instead of using GetPartsBoundsInBox() though, I’d reccomend using magnitude. It’s a perfect circle that’s very cheap to use (can be done every frame with minimal lag), and if you need height, you can just compared the Y positions.

2 Likes

Ah, I tried :GetPartsInsidePart() instead. :sob:

Anyway, it works, so thats cool.

1 Like