TD mouse collisions

task.wait(1)

local Placing = false
local GhostTower
local UIS = game:GetService(“UserInputService”)
local GhostMovement

function ShowPlaceable_Area(on)
if on == true then
for i, v in ipairs(game.Workspace.PlaceableArea:GetChildren()) do
if v:IsA(“BasePart”) then
v.Transparency = 0.7
end
end
else
for i, v in ipairs(game.Workspace.PlaceableArea:GetChildren()) do
if v:IsA(“BasePart”) then
v.Transparency = 1
end
end
end
end

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then
return
end

if input.UserInputType == Enum.UserInputType.MouseButton1 then
local mouse = game.Players.LocalPlayer:GetMouse()
if mouse.Target then
if mouse.Target.Parent.Name == “PlaceableArea” then
if Placing == true then
Placing = false

  			if GhostTower then
  				GhostTower:Destroy()
  				print("placingTower on: " .. tostring(mouse.Target))

  				if GhostMovement then
  					GhostMovement:Disconnect()
  				end

  				ShowPlaceable_Area(false)
  			end
  		end
  	end
  end

end
end)

for i, v in ipairs(script.Parent:GetDescendants()) do
if v:IsA(“ImageButton”) then
v.MouseButton1Click:Connect(function()
if Placing == false then
Placing = true
if v.Parent:FindFirstChild(“Tower”) then
GhostTower = game.ReplicatedStorage.Towers:FindFirstChild(v.Parent.Tower.Value):Clone()
GhostTower.Parent = game.Workspace
local mouse = game.Players.LocalPlayer:GetMouse()
GhostMovement = game:GetService(“RunService”).RenderStepped:Connect(function()
if GhostTower then
if mouse.Target then
GhostTower:PivotTo(
CFrame.new(mouse.Hit.Position) *
CFrame.new(0, GhostTower.PrimaryPart.Size.Y * 1.2, 0)
)
end
end
end)

  			ShowPlaceable_Area(true)
  		end
  	else
  		Placing = false
  		if GhostTower then
  			GhostTower:Destroy()
  		end
  		
  		if GhostMovement then
  			GhostMovement:Disconnect()
  		end
  		ShowPlaceable_Area(false)
  	end
  end)

end
end

The code pasted pretty weirdly but it is all connected.

Anyway, im making a tower placement system but i have only just started on the client side. when you click a button, it makes you drag a ghost tower( like in most td games ) until you click. but the cursor is hitting the ghost tower. once i get help with that there will probably be another problem. so the placeable area are basicly parts that you can place towers on. That is detected with mouse.Target but the ghost tower would be in the way

Hi! You should checkout TargetFilter. It makes it so that Mouse.Target (and Mouse.Hit) ignores the set TargetFilter instance and its descendants, perfect for your problem.
You only have to add a line of code that would look like this:

mouse.TargetFilter = GhostTower

EDIT: And also make sure that you also always update the TargetFilter to the current GhostTower

1 Like

Hey thanks I have never heard of that before but it worked! tysm for that!

1 Like

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