Placement System Using Attachments Creates Multiple Walls

Hello, this is my first time posting on the developer forums so sorry if I do this wrong. Also, I am still learning quite a bit but I love to challenge myself and find doing these really hard challenges I can learn a lot.
Anyway, I saw this post and its solution and thought I should give it a go since it seems like something I’d like to code and learn about. I started well and managed to get a fake blue semi-transparent wall to attach to the attachments on the foundation floor when a tool is equipped. You can see my code and the gif of what I mean below.

What I have so far (without the problem)

Picture of the attachments:

The Problem
The problem is when the player has the tool selected and the player clicks to place a wall it places multiple walls not only where the fake wall is but also on the example wall in the picture above. I found out that when the player clicks it repeats the function connected to MouseButton1Down a random amount of times. See below to see a gif of what happens. So far I have tried adding a debounce as I figured that should stop it firing multiple times but it hasn’t worked. My code will be below the gif. Any help would be greatly appreciated and I would love some feedback and ideas on what I should have done or should do to make this more efficient or just to solve problems.

My Code
So for this I have two scripts. One is a local script in the tool to detect when its equipped with the following code:

local RS = game:WaitForChild("ReplicatedStorage")
local ClickEvent = RS.PlacementSystem:WaitForChild("ClickToPlace")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local ToolEquiped = false
local Tool = script.Parent
	ToolEquiped = true

function Main()
	while ToolEquiped == true do
		local WallPos
		local MouseTarget = mouse.Target
		local target
		if MouseTarget then 
			target =
		if target == "Foundation-Floor" then
			local ClosestDistance = math.huge
			local ClosestAttachment
			for _,attachment in pairs(mouse.Target:GetChildren()) do
				if attachment.Name == "WallSnapAttachment" then
					local mousePos = mouse.Hit.p
					local worldPos = attachment.WorldPosition
					local distance = (mousePos - worldPos).Magnitude
					if distance < ClosestDistance then
					ClosestDistance = distance
					ClosestAttachment = attachment
			local floor = mouse.Target
			local wall = game.Workspace.FakeWall
			wall.Transparency = 0.75
			local wallCframe = floor.CFrame *
			wallCframe = wallCframe *
			wallCframe = wallCframe *CFrame.Angles(0, math.rad(ClosestAttachment.Orientation.Y), 0)
			wall.CFrame = wallCframe
			WallPos = wall.Position
			local wall = game.Workspace.FakeWall
			wall.Transparency = 1
			ToolEquiped = false
			local wall = game.Workspace.FakeWall
			wall.Transparency = 1
		local enabled = true
			if not enabled then return end
			enabled = false
			enabled = true

The other is a small script is server storage to place the real wall when the player clicks. I have a remote function which works perfectly as I tested it and I am quite confident with them.

local RS = game:WaitForChild("ReplicatedStorage")
local ClickEvent = RS.PlacementSystem:WaitForChild("ClickToPlace")

ClickEvent.OnServerEvent:Connect(function(Player, WallTargetPosition)
	local newWall = game.ServerStorage.Wall:Clone()
	newWall.Parent = game.Workspace
	newWall.Position = WallTargetPosition

Again any help would be greatly appreciated and I would love some feedback and ideas on what I should have done or should do to make this more efficient or just to solve problems. Just so you know once I find and fix the bugs I am going to make this be able to place other foundations that snap together as well as other walls like windows and door frames similar to rust. This is not for an actual game but meant to be a learning experience for me.

Thanks for taking your time to read this (which is probably a mess so sorry)
Edit1: I know I should use comments I just haven’t developed that habit as I get excited when coding and tend to rush things a bit


Sounds to me like you need to do some sanity checking.

When going through your code, you have absolutely no statements that prevent walls from being placed within another. Try fixing that, and seeing if that solves your issue.

1 Like

Yeah I do realise that I have no code that detects if a wall is already there, that was going to be what I coded next however when I tried the code I have now with one click multiple walls are duplicated. Which is my issue. I can try and code a collision check script and see if mulitple walls are still duplicated but I still don’t understand why with one click it will run the code to duplicate and move the wall 20-100 times (it seems to be random) and why the wall object that is behind the foundation also gets duplicated.
Edit: I will add a gif showing what happens it’s in the problem section now. Sorry I thought I added it, my mistake

1 Like

Your issue, after looking a bit more, is probably the fact that you’re binding the mouse clicking event multiple times. Move the mouse.Button1Down event out of the scope of the while loop.

1 Like

Ah okay, I will try and give that ago and if it works I’ll set this as the solution and let you know. Thanks.
Edit: It works great thanks, I didn’t realise it would do that.