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

How Can I Break This Loop? [SLOVED]

Hello!

I need your help to find how to break this loop in line 3 when I release mouse button:

script.Parent.Equipped:connect(function(mouse)
	mouse.Button1Down:connect(function()
		while mouse.Button1Down do
			wait()
			if mouse.Target.Name == "DrawBoard" then
					local instspr = Instance.new("Part")
				instspr.Parent = mouse.Target
				instspr.CFrame = CFrame.new(mouse.Hit.p) * CFrame.new(0, 0.1, 0)
				instspr.Anchored = true
				instspr.CanCollide = false
				instspr.BrickColor = BrickColor.new("Institutional white")
				instspr.Name = "DrawPix"
				instspr.FormFactor = Enum.FormFactor.Custom
				instspr.Size = Vector3.new(0.2, 0.2, 0.2)
				instspr.BottomSurface = Enum.SurfaceType.Smooth
				instspr.TopSurface = Enum.SurfaceType.Smooth
			end
		end
	end)
end)

1 answer

✓ Accepted answer

A debounce.

mouse.Button1Down:connect(function()
	down = true --Setting down to true before the loop starts
	while down do -- While down is true it will loop
	--stuff
	end
end)

mouse.Button1Up:connect(function()
	down = false --When button it released it sets down to false thus breaking the loop
end)

4 comments

0

Lol thanks.

0

Works1

0

Wouldn't you want to put the while loop outside the click event?

0

Yeah if you put it in a function then run that function every time the click event happens.

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