How can I add velocity to character when moving

I wanna make it so when I hold down W then a body force or anything of that sort gets inserted into the player and where ever the player is facing it will add acceleration to but im not sure how to do so because if then I look away then how am I gonna remove the not needed force from my character, this is just an idea I got for ropes when you swing on them

1 Like

Maybe check how long they’ve been holding W, and increase their WalkSpeed property the longer they’ve been holding it? This can then be decreased if they stop holding it.

Well, since you want to remove the acceleration and add it when you hold W, this can be simply made with UserInputService, InputBegan, and InputEnded. InputBegan is an event that fires when a key is pressed. If you want to find out if the player pressed W, check it when the event fires.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		-- code!
	end
end)

And in that code, you can do whatever you would add to the player or the character. Since you want to create a velocity when the player holds down W, there’s an object called BodyVelocity. It is simple to create a BodyVelocity, you just need to parent it to a part so the BodyVelocity works.

local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.Parent = game.Players.LocalPlayer.Character.HumanoidRootPart
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		local BodyVelocity = Instance.new("BodyVelocity")
        BodyVelocity.Parent = game.Players.LocalPlayer.Character.HumanoidRootPart
	end
end)

An example of the BodyVelocity’s parent can be the HumanoidRootPart as it’s the primary part for the character. Also, remember to change the velocity of the BodyVelocity so it moves where you want it to move!

Additionally, if you want to remove the BodyVelocity when they stop holding W, use the InputEnded event. Check what input had ended and if it was W, remove the BodyVelocity object.

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		-- code!
	end
end)
UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		if player.Character.HumanoidRootPart:FindFirstChildWhichIsA("BodyVelocity") then -- this checks if there is a BodyVelocity
			local BodyVelocity = player.Character.HumanoidRootPart.Velocity
			BodyVelocity:Destroy() -- deletes the BodyVelocity
		end
	end
end)
2 Likes

you didn’t have to delete your post lol

also i understand if OP wants to let mobile players join their game, so best to use ContextActionService and determine whether or not if the player is moving forward :+1:

How can I make it detect if player is looking up because I don’t want it to add any Y value to body velocity

Try:

local VelocityCap = 1

game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
while true do
wait(.1)
if Character.HumanoidRootPart.Velocity.Y < VelocityCap-1 then
Character.HumanoidRootPart.Velocity = Vector3.new(Character.HumanoidRootPart.Velocity.X, VelocityCap-1, Character.HumanoidRootPart.Velocity.Z)
end
end
end)
end)

1 Like