- What do you want to achieve?
I want to prevent towers from being placed inside other towers’ hitboxes while still allowing them to be placed right next to each other. - What is the issue?
Currently, placement is sometimes allowed inside other towers, and I want to ensure that placement is only blocked when inside a hitbox. - What solutions have you tried so far?
- Using a
Hitboxtag for tower parts. - Raycasting from above the placement point to check if it hits another tower.
- Ignoring characters and non-tower entities using
RaycastParams.
- Code Example
local ignoreList = {}
– Ignore all player characters
for _, character in ipairs(CollectionService:GetTagged(“Character”)) do
table.insert(ignoreList, character)
end– Ignore non-tower entities
for _, entity in ipairs(GameData.Entities:GetChildren()) do
if entity:GetAttribute(“Team”) ~= “Tower” then
table.insert(ignoreList, entity)
end
end– Validate tower placement on the server
local rayOrigin = cframe.Position + Vector3.new(0, 20, 0)
local rayDirection = Vector3.new(0, -50, 0)
local params = RaycastParams.new()
params.FilterDescendantsInstances = ignoreList
params.FilterType = Enum.RaycastFilterType.Blacklistlocal result = workspace:Raycast(rayOrigin, rayDirection, params)
– No surface detected
if not result then
Remotes.Message:FireClient(plr, “Invalid placement”, 3, Color3.new(1,0,0))
return
end– Hit a forbidden area (hitbox or no-place tag)
if CollectionService:HasTag(result.Instance, “Hitbox”) or CollectionService:HasTag(result.Instance, “NoPlace”) then
Remotes.Message:FireClient(plr, “Cannot place here”, 3, Color3.new(1,0,0))
return
end– Otherwise, placement is valid
– Insert tower spawning logic here