Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to continuously make bricks with Button1Down?

player = game.Players.LocalPlayer
mouse = player:GetMouse()

mouse.Button1Down:connect(function()
	repeat
		local new = Instance.new("Part", workspace)
		new.Position = Vector3.new(player.Character.Torso.Position)
		wait(.1)
	until mouse.Button1Up
end)

This is the code in a local script in StarterPack. When I hold down left click, it only makes one. Why?

1 answer

The problem is that mouse.Button1Up is the event and therefor is not considered nil or false which is why this will only run once.

For this you would need to keep track of the mouse state then stop the loop when the mouse is up:-

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local isLeftDown = false -- keep track of the mouse up/down state
local offset = Vector3.new(5,0,0) -- offset for the part

mouse.Button1Down:Connect(function()
	isLeftDown = false
	repeat
		local newPart = Instance.new('Part', game.Workspace)
		newPart.Position = plr.Character.Torso.Position + offset
		wait(0.5)
	until isLeftDown -- run this while the mouse is down
end)

mouse.Button1Up:Connect(function()
	isLeftDown = true -- mouse it up
end)

I hope this helps.

This discussion is preserved as it was. New answers, comments, and votes are closed.