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.