WASD Movement on UI

I wouldn’t recommend using this code as it has bad practices, is frame-dependent and some of it is unnecessary.

Here’s a better approach now that I know what you’re looking for:

local object = ... -- object to move, e.g. a frame
local MOVE_SPEED = 0.1 -- 10% of the screen every second

local function OnRenderStepped(deltaTime)
	-- check if a textbox is focused (like a chat), if yes => disable input
	local isTextBoxFocused = userInputService:GetFocusedTextBox() ~= nil
	
	-- calculate move input
	local moveInput = Vector2.new(
		(not isTextBoxFocused and userInputService:IsKeyDown(Enum.KeyCode.A) and -1 or 0)
			+ (not isTextBoxFocused and userInputService:IsKeyDown(Enum.KeyCode.D) and 1 or 0),
		(not isTextBoxFocused and userInputService:IsKeyDown(Enum.KeyCode.W) and -1 or 0)
			+ (not isTextBoxFocused and userInputService:IsKeyDown(Enum.KeyCode.S) and 1 or 0)
	)
	
	-- calculate velocity
	local velocity = UDim2.new(
		moveInput.X * MOVE_SPEED * deltaTime, 0, 
		moveInput.Y * MOVE_SPEED * deltaTime, 0
	)
	
	-- apply velocity (change in position)
	object.Position += velocity
end

runService.RenderStepped:Connect(OnRenderStepped)
1 Like