How to make it so I cant place towers on the path

Can someone help me figure this out there is no information on how to do this. Can someone help me?

Towers? On what path? We have even less info here

its a tower defense simulator game

Are you using a tile-based system? You could see if the cell the player is attempting to place a tower on is occupied by a path tile.

im using like small flat rectangles and waypoints

Could you give us your code, a image, or even a video to help us understand your code and how to achieve your goal?

I’d recommend looking at the #help-and-feedback:scripting-support topic template, since there isn’t much information here.

If your system is based on tiles, just record which tiles have path on them (in a table) then when the player wants to place a tower on a tile make sure the tile isn’t one of the ones in the table.

If your system is raycast based, just check if the RaycastResult.Instance is not a part of the path.

There are a lot of ways to do this and the question is very vague, if you add a picture, a few sentences of context, or some code we can probably help you better.

image

Where is the placement system? Do you have one? How does it function? Could you provide your code?

Edit:
Adding on to what @PersonifiedPizza said above, if your using raycasts you can check to see if the


Raycast.Instance.Parent.Name == “Path”

Or something like that. :sweat_smile:

When the player clicks on their mouse, see if the player clicked on the path. If they did, don’t place a tower, if it’s not, place a tower.

I’m on mobile so I can’t type some code, but basically add click detectors on the ground and not on the paths.

Also to get the mouse location you need to type

game.Players.LocalPlayer:GetMouse().Position

1 Like

no one can help without the script

1 Like

local Remote = game.ReplicatedStorage[“ReplicatedStorage_CLOUD”].UnitControls.PlaceUnit
local Workspace = game:GetService(“Workspace”)
local Map = Workspace:WaitForChild(“Map”)

Remote.OnServerEvent:Connect(function(Player, Unit, Hit, Rotation, Can)
local Currency = “Money”
local Price = nil
local Level = “0”

Unit = game.ReplicatedStorage["ReplicatedStorage_CLOUD"].Units[Unit.Name].Levels[Level].Object[Unit.Name]
Price = game.ReplicatedStorage["ReplicatedStorage_CLOUD"].Units[Unit.Name].Levels[Level].Cost.Value

if Can == true then
	local Towers = 0
	for _, v in pairs(Workspace:GetChildren()) do
		if v:FindFirstChild("Configuration") then
			if v.Configuration:FindFirstChild("Owner") then
				if v.Configuration.Owner.Value == Player.Name then
					Towers = Towers + 1
				end
			end
		end
	end
	if Towers >= Map:WaitForChild("Configuration"):WaitForChild("TowersLimit").Value then
		game.ReplicatedStorage["ReplicatedStorage_CLOUD"].GameNotification:FireAllClients(Player, "Unit Editor", "You've reached towers limit")
	else
		if game.Players[Player.Name].leaderstats[Currency].Value >= Price then
			game.Players[Player.Name].leaderstats[Currency].Value = game.Players[Player.Name].leaderstats[Currency].Value - Price
			Unit = Unit:Clone()
			
			local ConfigFolder = game.ReplicatedStorage["ReplicatedStorage_CLOUD"].Units[Unit.Name].Levels[Level].Settings:Clone()
			ConfigFolder.Name = "Configuration"
			ConfigFolder.Parent = Unit
			
			Unit.Parent = game.Workspace
			
			if Unit:IsA("Model") then
				if Unit:FindFirstChild("Humanoid") then
					
				else
					for i, v in pairs(Unit:GetChildren()) do
						if v:IsA("Part") or v:IsA("MeshPart") then
							v.Anchored = true
						end
					end
				end
			else
				Unit.Anchored = true
			end
			if Unit:IsA("Model") then
				if Unit:FindFirstChild("Humanoid") then
					
				else
					for i, v in pairs(Unit:GetChildren()) do
						if v:IsA("Part") or v:IsA("MeshPart") then
							v.Transparency = 0
						end
					end
				end
			else
				Unit.Transparency = 0
			end
			if Unit:IsA("Model") then
				for i, v in pairs(Unit:GetChildren()) do
					if v:IsA("Part") or v:IsA("MeshPart") then
						v.CanCollide = true
					end
				end
			else
				Unit.CanCollide = true
			end
			if Unit:IsA("Model") then
				-- Cannot rotate group
			else
				Unit.Orientation = Rotation
			end
			if Unit:IsA("Model") then
				Unit:SetPrimaryPartCFrame(CFrame.new(Hit.Position))
				Unit:SetPrimaryPartCFrame(CFrame.new(Unit.PrimaryPart.Position.X + Unit.PrimaryPart.Size.X / 2, Unit.PrimaryPart.Position.Y + Unit.PrimaryPart.Size.Y / 2, Unit.PrimaryPart.Position.Z + Unit.PrimaryPart.Size.Z / 2))
			else
				Unit.Position = Hit.Position
				Unit.Position = Vector3.new(Unit.Position.X + Unit.Size.X / 2, Unit.Position.Y + Unit.Size.Y / 2, Unit.Position.Z + Unit.Size.Z / 2)
			end
		    if Unit:FindFirstChild("ClickDetector") then
				Unit.ClickDetector.MaxActivationDistance = 10
			end
			if Unit:IsA("Model") then
				Unit[Unit.PrimaryPart.Name].PlaceSound:Play()
			else
				Unit.PlaceSound:Play()
			end
			if Unit:FindFirstChild("Configuration") then
				if Unit.Configuration:FindFirstChild("Owner") then
					Unit.Configuration.Owner.Value = Player.Name
				end
			end
		else
			game.ReplicatedStorage["ReplicatedStorage_CLOUD"].GameNotification:FireAllClients(Player, "Unit Editor", "Not enough ".. Currency.. " to place this item")
		end
	end
else
	game.ReplicatedStorage["ReplicatedStorage_CLOUD"].GameNotification:FireAllClients(Player, "Unit Editor", "You tried place this item too far away from you")
end

end)

Use the player’s mouse to identify the part it is currently being targeting on using render service. (connection)
(PS: Use TargetFilter and filter the tower so it wont go towards your cam)
Then add some code that basically identifies if whether the part is a path part or not.
If it is not, when you do a Mouse.Button1Down event it will go through and make sure to disconnect the connection to avoid FPS drops.
If it is a path part, it will not be placed (assuming theres a value that sets it to false)

You just need to raycast down from the position and check if the raycast hits the path or not.

https://developer.roblox.com/en-us/articles/Raycasting


Edit:

Also when the camera’s CFrame changes too :+1:

I don’t think there should be an FPS drop unless it’s creating multiple connections (which it shouldn’t). There should only be one connection to Button1Down, so you shouldn’t need to disconnect it (unless you don’t use a debounce). Disconnecting instead of using a denounce and using a denounce have basically the same performance in this case though.

1 Like

https://developer.roblox.com/en-us/api-reference/event/Mouse/Move

Instead of an event which is fired every frame I’d recommend to listen for the “.Move” event to fire on the mouse object as this will occur a lot less frequently and for this task it is all that is necessary.