I have a gui to move a certain direction. It works fine for the first few times i press the button, but after a few more times of pressing the button, it moves faster. Why?

local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input) 
	while wait() do
		if uis:IsKeyDown(Enum.KeyCode.Left) then
			script.Parent.Position = script.Parent.Position - UDim2.new(0, 2, 0, 0)
			print(script.Parent.Position.X.Offset)
		elseif uis:IsKeyDown(Enum.KeyCode.Right) then
			script.Parent.Position = script.Parent.Position + UDim2.new(0, 2, 0, 0)
		end
	end
end)

3 comments

0

The inputbegan event creates new while loops everytime you press a button

0

is there a way to break the loop whenever it ends?

0

like what cars said, just use the break function for loops to break it.