How do you detect how fast is player moving

So i’ve been trying to make a flying script and if player crashes at very high speed the player dies but i’ve ran into an issue where i don’t know how do i detect Player’s Speed while flying

--//Services
local Context_Action_Service = game:GetService("ContextActionService")
local Run_Service = game:GetService("RunService")

--//Connections
local Fly_Connection = nil
local Crashed = nil

--//Player
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Root = Character:WaitForChild("HumanoidRootPart")

--//Gravity
local YAxis = 0
local Force = 400
local Drag = 1

local Gravity_Vector = Vector3.new(0, workspace.Gravity, 0)

local Vector_Force = script:WaitForChild("VectorForce")
Vector_Force.Attachment0 = Root:WaitForChild("RootRigAttachment")

local Align_Orientation = script.AlignOrientation
Align_Orientation.Attachment0 = Root:WaitForChild("RootRigAttachment")

--| Main Script | --

local function Fly_Action(Action_Name, Input_State, Input_Object)
	if Input_State ~= Enum.UserInputState.Begin then return Enum.ContextActionResult.Pass end
	if Fly_Connection == true then return Enum.ContextActionResult.Pass end
	if Fly_Connection == nil then
		Fly_Connection = true
		if Character.Humanoid.FloorMaterial ~= Enum.Material.Air then
			Character.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			task.wait(0.1)
		end
		
		Vector_Force.Enabled = true
		Align_Orientation.CFrame = Root.CFrame
		Align_Orientation.Enabled = true
		Character.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
		Fly_Connection = Run_Service.Heartbeat:Connect(function(Delta_Time)
		Vector_Force.Force = Gravity_Vector * Root.AssemblyMass
			
			local Move_Vector = Vector3.new(Character.Humanoid.MoveDirection.X, YAxis, Character.Humanoid.MoveDirection.Z)
			
			if Move_Vector.Magnitude > 0 then
				Move_Vector = Move_Vector.Unit
				Vector_Force.Force += Move_Vector * Force * Root.AssemblyMass
				
				if math.abs(Move_Vector.Y) == 1 then
					Align_Orientation.CFrame = CFrame.lookAt(Vector3.new(0, 0 ,0), Move_Vector, -Root.CFrame.LookVector) * CFrame.fromOrientation(-math.pi / 2, 0, 0)
				else
					Align_Orientation.CFrame = CFrame.lookAt(Vector3.new(0, 0 ,0), Move_Vector) * CFrame.fromOrientation(-math.pi / 2, 0, 0)
				end
				
				if Crashed == nil then
					for _, v in ipairs(Character:GetChildren()) do
						if v:IsA("MeshPart") then
							local previousV = v
							if v:IsA("Accessory") then
								v = v:WaitForChild("Handle")
							end
							v.Touched:Connect(function(hit)
								Crashed = true
								if hit.Parent == workspace.HitObjects then
									Character.Humanoid.Health = 0

									v = previousV
									Crashed = nil
								end
							end)
						end
				    end
				end
			end
			if Root.AssemblyLinearVelocity.Magnitude > 0 then
				local Drag_Vector = -Root.AssemblyLinearVelocity.Unit *  Root.AssemblyLinearVelocity.Magnitude ^ 1.2
				Vector_Force.Force += Drag_Vector * Drag * Root.AssemblyMass
			end
		end)
	else
		Vector_Force.Enabled = false
		Align_Orientation.Enabled = false
		Character.Humanoid:ChangeState(Enum.HumanoidStateType.Freefall)
		Fly_Connection:Disconnect()
		Fly_Connection = nil
		Crashed = nil
		
	end
	return Enum.ContextActionResult.Pass
end

local function Up_Action(Action_Name, Input_State, Input_Object)
	if Input_State == Enum.UserInputState.Begin then YAxis = 1 else YAxis = 0 end return Enum.ContextActionResult.Pass
end

local function Down_Action(Action_Name, Input_State, Input_Object)
	if Input_State == Enum.UserInputState.Begin then YAxis = -1 else YAxis = 0 end return Enum.ContextActionResult.Pass
end

Context_Action_Service:BindAction("Fly", Fly_Action, true, Enum.KeyCode.F)
Context_Action_Service:SetTitle("Fly", "Fly")
Context_Action_Service:SetPosition("Fly", UDim2.new(1, -150, 1, -80))

Context_Action_Service:BindAction("Up", Up_Action, true, Enum.KeyCode.E)
Context_Action_Service:SetTitle("Up", "Up")
Context_Action_Service:SetPosition("Up", UDim2.new(1, -55, 1, -145))

Context_Action_Service:BindAction("Down", Down_Action, true, Enum.KeyCode.Q)
Context_Action_Service:SetTitle("Down", "Down")
Context_Action_Service:SetPosition("Down", UDim2.new(1, -105, 1, -145))
1 Like
local Speed = Root.AssemblyLinearVelocity.Magnitude

it doesn’t work since player HumanoidRoot will always have AssemblyLinearVelocity of 0, 0, 0

1 Like

why are you checking if its magnitude is above zero if it is constant 0

1 Like

because i only found it out right now
Edit: Never mind i was wrong i tried printing players speed and it actually changes

1 Like

could you provide a video/gif showing how this works?
edit: nvm i thought something was wrong

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.