I’m not talking about the walkSpeed property in the humanoid, I want my code to detect the exact speed the player is going at (so it returns 0 if they aren’t walking, 16 if they walk and something like 50 for example if the player is on a platform that’s sliding on a ramp).
As I said in the title, I will also need to detect if the player is in the air, it will return true if the player is jumping or falling no matter what.
For the speed, you just check the HumanoidRootPart’s velocity
speed = Player.Character.PrimaryPart.Velocity.Magnitude
For detecting in the air, you just need to do some raycasting, fire a ray ‘down’ from the HumanoidRootPart’s position (having the character excluded from the testing) and if the ray hit’s something, and it is greater than the Humanoid’s HipHeight + (HumanoidRootPart.Size.Y /2) + 1 (a little wiggle room) then either do … InAir = true, or HeightFromGround = HumanoidRootPart.Position - Hit.Position
This is the best way to get the character’s speed, but to check if the character is grounded you can simply check the Player.Character.Humanoid.FloorMaterial value. It will be nil when the player is in the air or a material when the player is touching the ground.
To get the speed that the player is traveling vertically to check whether the player is falling or jumping, just use the Player.Character.PrimaryPart.Velocity.Y value. It will be > 0 when the player is jumping and floor material is nil, or < 0 when the player is falling back down.
Great suggestions! Also, I did not know that about the .FloorMaterial, so I learned something today XD
Thanks
Are you sure it’s FloorMaterial? Every time I tried to check if a player is in the air with “if h.FloorMaterial==nil”, it would never work after jumping or falling.
Print out what h.FloorMaterial is equal to in a loop to see what values it can be.
It prints “Enum.Material.Air” when jumping or falling and “Enum.Material.Plastic” when the player is on the ground
I actually found out about the FloorMaterial issue I was having:
https://create.roblox.com/docs/reference/engine/enums/Material
If you use “if h.FloorMaterial.Value==1792” or “if h.FloorMaterial.Name==‘Air’”, it will actually work and check if the player is in the air, thank you guys anyway for the help you were able to give.
I’m actually working on this myself. Basically, you do the following from the server:
local function detectSpeed(player)
local char = player.Character
local root = char:FindFirstChild("HumanoidRootPart")
local spos = root.Position
task.wait(0.1)
local epos = root.Position
return (spos - epos).Magnitude
end
What this does is it takes the position of the player’s root part and waits for 0.1 seconds and takes it again. Then from the two positions it creates a vector and takes the magnitude of that vector. The magnitude is the player’s current speed. For 16 studs/sec, the max they can travel in 0.1 seconds is 1.6 studs. It can be more if they are falling.
To see if they are on the ground or not, you can use raycast:
local function onGround(player)
local char = player.Character
local root = char:FindFirstChild("HumanoidRootPart")
local params = RaycastParams.new()
local vector = Vector3.new(0, -10, 0)
local result = game.Workspace:Raycast(root.Position, vector, params)
if result ~= nil then
if result.Distance < 3 then
return true
end
end
return false
end
This function takes the player, then from the position of the player’s root part performs a raycast downward. If the result is not nil, then it looks at the distance. If the distance is less than 3 studs, then the player is judged to be on the ground and true is returned. If anything is off or the raycast returns nil then false is returned.