I made a script to move a gui by moving the arrow keys, and everything works just like expected, but not really. First: when i move gui with the arrow keys in two different directions, for example up and left at the same time, its really glitchy. Plz helps.

script:

local uis = game:GetService("UserInputService")

local currentposition = script.Parent.Position

function MoveLeft()
	local moving = false	
	uis.InputBegan:Connect(function(input, gameprocessedevent) 
		if input.KeyCode == Enum.KeyCode.Left then
			moving = true
			while moving == true do
				wait(0.01)
				script.Parent.Position = script.Parent.Position - UDim2.new(0, 7, 0, 0)
			end
		end
	end)
	uis.InputEnded:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.Left then
			moving = false
		end
	end)
end

function MoveRight()
	local moving = false
	uis.InputBegan:Connect(function(input, gameprocessedevent) 
		if input.KeyCode == Enum.KeyCode.Right then
			moving = true
			while moving == true do
				wait(0.01)
				script.Parent.Position = script.Parent.Position + UDim2.new(0, 7, 0, 0)	
			end
		end		
	end)
	uis.InputEnded:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.Right then
			moving = false
		end
	end)
end

function MoveUp()
	local moving = false
	uis.InputBegan:Connect(function(input, gameprocessedevent) 
		if input.KeyCode == Enum.KeyCode.Up then
			moving = true
			while moving == true do
				wait(0.01)
				script.Parent.Position = script.Parent.Position - UDim2.new(0, 0, 0, 5)	
			end
		end		
	end)
	uis.InputEnded:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.Up then
			moving = false
		end
	end)
end

function MoveDown()
	local moving = false
	uis.InputBegan:Connect(function(input, gameprocessedevent) 
		if input.KeyCode == Enum.KeyCode.Down then
			moving = true
			while moving == true do
				wait(0.01)
				script.Parent.Position = script.Parent.Position + UDim2.new(0, 0, 0, 5)	
			end
		end		
	end)
	uis.InputEnded:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.Down then
			moving = false
		end
	end)
end

MoveLeft()
MoveRight()
MoveDown()
MoveUp()

6 comments

0

What are you wanting to see? (having 2+ buttons pressed at the same time)

0

im trying to get it to not glitch whenever its moving, when it moving, it tends to not move smoothly

0

You will need to restructure the way you handle movement. Instead of each direction handling movement on its own, just track what arrow keys the user is pressing. Then just handle those keypresses in one function every frame.

0

how would i do that?

0

Is the script in script or localscript?

0

local