Help with linear velocity

Im trying to create a custom movement system, and things are working but theres a bug i cant seem to fix

The player slowly descends downward. Im aware that the cause is the Y value of the linear velocity is affected but i tried constantly setting it to 0 but that didnt work either (it just errored)

heres my code so far

local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")

local char = script.Parent
local HRP = char.HumanoidRootPart
local attachment = Instance.new("Attachment", HRP)

local LV = Instance.new("LinearVelocity", HRP)
local AV = Instance.new("AngularVelocity", HRP)
local speed = 0
LV.Attachment0 = attachment
LV.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
LV.MaxForce = math.huge

AV.Attachment0 = attachment
AV.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
AV.MaxTorque = math.huge

LV.Enabled = false
AV.Enabled = false
attachment.Name = "MoverAttachment"

UIS.InputBegan:Connect(function(input, chat)
	if chat then return end
	
	while UIS:IsKeyDown(Enum.KeyCode.W) do
		speed = speed + 1
		print("W is being held down!")
		task.wait()
		LV.Enabled = true
		LV.VectorVelocity = attachment.WorldCFrame.LookVector * speed
		if speed >= 20 then
			speed = 20
		end
	end
end)


im not sure why this is happening, and im really new to something as complex as movement systems so help is appreciated

thank you for your time

1 Like

try replacing this part with this

LV.VectorVelocity = attachment.WorldCFrame.LookVector * speed * Vector3.new(1,0,1)
1 Like

i tried this just now, it didnt work

What do you mean by “didn’t work”. You mean the character is still descending?

You are not using UserInputService properly. You should only use it to get inputs, then use that input somewhere else, not in the same function.

for every key you press, this runs

Your code may crash. You should do this instead

local RunService = game:GetService("RunService")
local wPressed = false
UIS.InputBegan:Connect(function(input, chat)
	if chat then return end
	
	if input.KeyCode == Enum.KeyCode.W then
		wPressed = true
	end
end)
UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		wPressed = false
		speed = 0 -- Set speed back to 0?
	end
end)

RunService.Stepped:Connect(function()
	if wPressed then
		speed = speed + 1
		print("W is being held down!")
		task.wait()
		LV.Enabled = true
		LV.VectorVelocity = attachment.WorldCFrame.LookVector * speed
		if speed >= 20 then
			speed = 20
		end
	end
end)

or if you really want it in a single function, then you have to check for key press first before looping.

UIS.InputBegan:Connect(function(input, chat)
	if chat then return end

	if input.KeyCode == Enum.KeyCode.W then
		while UIS:IsKeyDown(Enum.KeyCode.W) do
			speed = speed + 1
			print("W is being held down!")
			task.wait()
			LV.Enabled = true
			LV.VectorVelocity = attachment.WorldCFrame.LookVector * speed
			if speed >= 20 then
				speed = 20
			end
		end
	end
end)
1 Like

yes! thats what i meant! sorry for the misunderstanding

local UserInput = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")

local Attachment = Instance.new("Attachment")
Attachment.Parent = HRP
local LV = Instance.new("LinearVelocity")
LV.Parent = HRP
local AV = Instance.new("AngularVelocity")
AV.Parent = HRP

LV.Attachment0 = Attachment
LV.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
LV.MaxForce = math.huge

AV.Attachment0 = Attachment
AV.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
AV.MaxTorque = math.huge

LV.Enabled = false
AV.Enabled = false

local Scalar = 0
local Connection

UserInput.InputBegan:Connect(function(StartInput, _)
	if StartInput.KeyCode == Enum.KeyCode.W then
		if Connection then return end
		Connection = UserInput.InputEnded:Connect(function(EndInput, _)
			if EndInput.KeyCode == Enum.KeyCode.W then
				Connection:Disconnect()
				Connection = nil
				LV.Enabled = false
				AV.Enabled = false
				Scalar = 0
			end
		end)
		LV.Enabled = true
		AV.Enabled = true
		while true do
			task.wait()
			if Connection then
				Scalar = math.min(Scalar + 1, 20)
				LV.VectorVelocity = Attachment.WorldCFrame.LookVector * Scalar
				AV.AngularVelocity = -Attachment.WorldCFrame.LookVector * Scalar
			else
				break
			end
		end
	end
end)

Feel free to remove the AV.AngularVelocity assignment expression, this seems to be working on my end.

2 Likes

aha if that helped you when are you going to give the solution to @Lielmaster

3 Likes