How to see if the player is no longer pressing a key?

So I’m trying to make a custom movement system for my game and I’m using body velocity (for reasons that make the game what it is)

It’s working fine but you can’t stop when you start to go. I tried searching this up a bit ago but came to no avail.

Any help?

-- getting the player and stuff

local player = game:GetService(“Players”).LocalPlayer
local char = workspace:FindFirstChild(player.Name)
local bodyvelocity = char:WaitForChild(“Body”).BodyVelocity
local camera = workspace.CurrentCamera

repeat wait()
	camera.CameraType = Enum.CameraType.Scriptable
until camera.CameraType == Enum.CameraType.Scriptable

-- settings up the system

local tweenservice = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")

-- Actual System Script

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.W then
			tweenservice:Create(bodyvelocity, TweenInfo.new(.5), {Velocity = Vector3.new(5,0,0)}):Play()
		elseif not input.KeyCode == Enum.KeyCode.W then
			tweenservice:Create(bodyvelocity, TweenInfo.new(.5), {Velocity = Vector3.new(0,0,0)}):Play() -- Doesnt work for some reason?
		end
	end
end)


while wait() do
	camera.CFrame = char:WaitForChild("CamPart").CFrame
end
1 Like

You have to use an InputEnded function.

Here is an article on all the events. UserInputService | Roblox Creator Documentation

1 Like

There is something called InputEnded:

UIS.InputEnded:Connect(function(output)

end)
1 Like

What I do is have a variable. On InputBegan, make the variable true. On InputEnded, make it false. Basically, you have a toggle, so you can always see if someone is holding down a key.

repeat
--a simple repeat loop, until they stopped holding it
until Variable == false