Placement System not working properly

Hello, I’m trying to create a placement system, However, it works fine as I wanted it to, but something weird happens.
–Client Code

local Walls = game.ReplicatedStorage:WaitForChild("Walls")

local Structure = game.ReplicatedStorage:WaitForChild("Structure")

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()


local UIS = game:GetService("UserInputService")

local Frame = script.Parent

local Mouse = Player:GetMouse()





for number, button in pairs(Frame:GetChildren()) do
	if button:IsA("TextButton") then
		button.MouseButton1Up:Connect(function()
			local PlacingStructure = false
			if PlacingStructure == false then
				PlacingStructure = true
				 ClientStructure = Walls:FindFirstChild(button.Name):Clone()
				print("Cloned!")

				

				ClientStructure.BrickColor = BrickColor.new("Lime green")
				ClientStructure.Material = Enum.Material.Neon
				ClientStructure.CanCollide = false
				ClientStructure.Parent = game.Workspace
				canplace = false

			end

			RunService.Stepped:Connect(function()
				if PlacingStructure == true then
					local MouseRay = Mouse.UnitRay
					local CastRay = Ray.new(MouseRay.Origin, MouseRay.Direction * 1000)
	
					local ignoreList = {ClientStructure, Character}					

					local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(CastRay, ignoreList)

		

					ClientStructure.Position = Vector3.new(position.X, position.Y + 5, position.Z)


					if hit and hit:IsA("Terrain") and (Character:WaitForChild("HumanoidRootPart").Position - ClientStructure.Position).magnitude < 50 then
						canplace = true	
						ClientStructure.BrickColor = BrickColor.new("Lime green")
					else
						canplace = false
						ClientStructure.BrickColor = BrickColor.new("Crimson")
					end

				end


			end)--Run Service
	
			Mouse.Button1Up:Connect(function()
				if canplace == true then
					print("Ready to place")
					local Result = Structure:InvokeServer(ClientStructure.Name, ClientStructure.CFrame)
					if Result == true then
						ClientStructure:Destroy()
						PlacingStructure = false
					end
				else
					print("Not in range to place")
					PlacingStructure = true
				end
				
			end)
		end) 
	end
end 

– Server code:

local Walls = game.ReplicatedStorage:WaitForChild("Walls")

local Structure = game.ReplicatedStorage:WaitForChild("Structure")

Structure.OnServerInvoke = function(Player, StructureName, StructureCFrame)
	local crafted = false
	local RealStructure = Walls:FindFirstChild(StructureName):Clone()
	RealStructure.CFrame = StructureCFrame
	RealStructure.Parent = game.Workspace
	crafted = true
	return crafted

end

This is the weird thing that happens when I place the 1st structure, it works fine, but when I click on other structure after I have placed the 1st one, it clones the 2nd one by how many times I have placed.

The problem is that when you click the place button it fires the Mouse.Button1Up:Connect(function() event. And it keeps listening for the mouse event. When you click the place button second time it fires the event again and now theres 2 listeners for the Mouse.Button1Up:Connect(function() event. You can disable the connection of the event when the player fires it.

local connection
connection = Mouse.Button1Up:Connect(function()
if canplace == true then
print(“Ready to place”)
local Result = Structure:InvokeServer(ClientStructure.Name, ClientStructure.CFrame)
if Result == true then
ClientStructure:Destroy()
PlacingStructure = false
end
else
print(“Not in range to place”)
PlacingStructure = true
end
connection:Disconnect()
end)

1 Like