Setting a value based on holding keys?

Hello there, I am trying to create a first-person custom character controller and can’t figure out the movement. I tried using this script:

local UserInputService = game:GetService("UserInputService")
local x = 0
local y = 0

function xMove()
	
	UserInputService.InputBegan:Connect(function(Input, IsTyping)
		
		if not IsTyping then
			
			if Input.KeyCode == Enum.KeyCode.A or Input.KeyCode == Enum.KeyCode.Left then
				
				x = -1
			
			elseif Input.KeyCode == Enum.KeyCode.D or Input.KeyCode == Enum.KeyCode.Right then

				x = 1
				
			else
				
				x = 0

			end
			
		end
		
	end)
	
end

while wait() do
	
	xMove()
	
	print(x)
	
end

as you can see I am using a function for moving in the x-direction which is going to be left and right where you are looking but whenever I click left the x value stays -1 until I click something else. Sorry I’m not a good scripter and don’t use UserInputSystem too much. I thought about using disconnected but don’t know how that works.