I dont know why but this script keeps putting down a tower instead of one part. Its suppose to loop and make it so I put one part down at a time but instead it puts down a ton of them stacked on each other and then stops.

Code...

	wait(2)
	local player = game.Players.LocalPlayer
	local mouse = player:GetMouse()
	local blankPart = game.Players.LocalPlayer.PlayerGui.BlockGui.Block.Part
	blankPart.Anchored=true
    blankPart.CanCollide=false
	blankPart.Transparency=0.75

	local mouseDown = false
	local connection
	 
	connection = mouse.Move:Connect(function() 
	    if not mouseDown then
	     --wait(0.1)
	        local part = blankPart:Clone() --never change the original part, only a clone
	        part.Parent=workspace
	        mouse.TargetFilter=part --ignore the clone while getting mouse position
	        local moveConn
	        moveConn = mouse.Move:Connect(function()
	            local p = mouse.Hit.p
	            part.Position = Vector3.new(math.floor(p.X),math.ceil(p.Y),math.floor(p.Z)) --mouse position in whole numbers
	        end)
	        mouse.Button1Down:Wait() --do not have to connect a click function :)
	        moveConn:Disconnect() --stop moving the clone
	        part.CanCollide=true
	        part.Transparency=0
	    end
	end)
	 
	mouse.Button1Down:Connect(function()
	    mouseDown = true
	    connection:Disconnect() 
	end)		

8 comments

0

You’re creating a new part every time the mouse moves.

1

Oh how do I fix it?

0

Make an if statement to check if a part is in workspace.

1

So if game.workspace.Part then (code)?

1

and If game.workspace.Part = nil then (create part)?

1

But I want if to create a lot of parts, not just one. So I can keep putting more down.

0

How do I make it so that I can see the part moving when I move the mouse but then I place it , and then I can do it again instead of once?

0

Try redefining connection on line 12 as mouse button 1click or something instead of move idk why move