Flight velocity movement issue

Code:

local Char = script.Parent

local UIS = game:GetService("UserInputService")

local ToggleKey = Enum.KeyCode.F

local Toggled = false

local CurrentSpd = 0

UIS.InputBegan:Connect(function(input, GPE)
	
	if not GPE then
		
		if input.KeyCode == ToggleKey then
			
			if Toggled == false then
				
				Toggled = true
				
				local LV = Instance.new("LinearVelocity")
				LV.Name = "FlightVelocity"
				LV.Parent = Char.HumanoidRootPart
				LV.MaxForce = math.huge

				local Att = Instance.new("Attachment")
				Att.Name = "FlightAtt"
				Att.Parent = Char.HumanoidRootPart
				
				LV.Attachment0 = Att
				
			else
				
				Toggled = false
				
				Char.HumanoidRootPart.FlightVelocity:Destroy()
				
				Char.HumanoidRootPart.FlightAtt:Destroy()
				
			end
			
		end
		
	end
	
end)

local plr = game.Players.LocalPlayer

local Mouse = plr:GetMouse()

Mouse.WheelForward:Connect(function()
	
	if Toggled == true then
		
		CurrentSpd += 0.5
		
	end
	
end)

Mouse.WheelBackward:Connect(function()
	
	if Toggled == true then

		CurrentSpd -= 0.5

	end

end)

What I pretty much want is that when I hold W only then it goes forward and when I hold W and A it goes forward and left esc. (while also keeping the Y axis speed which is CurrentSpd)

2 Likes

I have a simple project that uses the Heartbeat event to periodically update the VectorVelocity direction based on the HumanoidRootPart direction. I hope this can help you. Here’s how the script would look:

local Char = script.Parent

local UIS = game:GetService("UserInputService")

local ToggleKey = Enum.KeyCode.F

local Toggled = false

local CurrentSpd = 0

local LV = false

local function UpdateMovement()
	if Toggled and LV then
		local moveDirection = Char.HumanoidRootPart.CFrame.LookVector * CurrentSpd
		LV.VectorVelocity = Vector3.new(moveDirection.X, LV.VectorVelocity.Y, moveDirection.Z)
	end
end

game:GetService("RunService").Heartbeat:Connect(UpdateMovement)

UIS.InputBegan:Connect(function(input, GPE)

	if not GPE then

		if input.KeyCode == ToggleKey then

			if Toggled == false then

				Toggled = true

				LV = Instance.new("LinearVelocity")
				LV.Name = "FlightVelocity"
				LV.Parent = Char.HumanoidRootPart
				LV.MaxForce = math.huge

				local Att = Instance.new("Attachment")
				Att.Name = "FlightAtt"
				Att.Parent = Char.HumanoidRootPart

				LV.Attachment0 = Att

			else

				Toggled = false

				Char.HumanoidRootPart.FlightVelocity:Destroy()

				Char.HumanoidRootPart.FlightAtt:Destroy()

			end

		end
	
	end
	
end)

local plr = game.Players.LocalPlayer

local Mouse = plr:GetMouse()

Mouse.WheelForward:Connect(function()

	if Toggled == true then

		CurrentSpd += 0.5

	end

end)

Mouse.WheelBackward:Connect(function()

	if Toggled == true then

		CurrentSpd -= 0.5

	end

end)
1 Like

you can get the move direction vector by either humanoid.MoveDirection(idk if it works in seats) or like this

local direction = Vector3.zero
if UserInputService:IsKeyDown(Enum.KeyCode.W) then
     direction+=Vector3.new(0,0,1)--Vector3.zAxis
elseif UserInputService:IsKeyDown(Enum.KeyCode.A) then
     direction-=Vector3.new(1,0,0)
elseif --etc...
end
direction = direction.Unit--so that you dont move faster diagonally

then just set the velocity to (direction.X*spd,CurrentSpd,direction.Z*spd)

1 Like