Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[Solved] How would I detect how far and fast a player is falling?

So, I'm trying to make a script that when a player is falling for a certain amount of time and depending on their speed, an effect plays. Essentially, when a player is falling at a certain speed I want an effect to play, So screen shaking and Distortion. My Issue here is that I don't know how to detect how fast a player is falling. Would I use body velocity? and what Body velocity would I look for? I'm aware I can use Humanoid.FreeFall to detect falling but how would I know how far and the velocity of the player? Here's what I have:

local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

Humanoid.StateChanged:Connect(function(_, new)
	if new == Enum.HumanoidStateType.Freefall then
		print("Falling")
	end
end)

Edit: one last thing, if I were to use velocity what would I get the velocity of? The Torso? HumanoidRootPart? or something else?

just ended up doing this and it worked.

local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local vel = game.Players.LocalPlayer.Character.Torso.Velocity

Humanoid.StateChanged:Connect(function(_, new)
	if new == Enum.HumanoidStateType.Freefall then
		print("Falling")
		wait(1)
		vel = game.Players.LocalPlayer.Character.Torso.Velocity
		if vel.Y < -100 then
			game.Players.LocalPlayer.Character.Head.Fall:Play()
			game.Players.LocalPlayer.Character.Head.Fall.Looped = true
		end
	end
end)

the wait(1) could be improved, I'm still working on it. I'll update later.

1 answer

You can probably get the position and time from when the humanoid is in the freefall state and in the landed state.

Okay so it's been a bit since I've started writing this answer and I've been playing around. I think I've got it.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")
local HRP = char:WaitForChild("HumanoidRootPart")
local currentTime
local pos

local function checkEffect()
	local x = 1/currentTime 
	pos = math.abs(pos*x) -- multiply absolute value of distance (since the player fell, the distance is negative) x to calculate speed/sec
	print("speed:",math.floor(pos + 0.5),"studs / sec") --math.floor rounds to the nearest whole
	print("distance:",math.floor(pos/x + 0.5),"studs") 
	--you can do an if statement here to see if the effect is activated
end

Humanoid.StateChanged:Connect(function(_, new)
	if new == Enum.HumanoidStateType.Freefall then
		currentTime = tick()
		pos = HRP.Position.Y
	end
	if new == Enum.HumanoidStateType.Landed then
		currentTime = (tick()-currentTime) --gets the time in between falling and landing
		pos = HRP.Position.Y - pos --gets the vertical distance between when the humanoid started falling and when it landed
		checkEffect() 
	end
end)

Let me know if this helps.

This discussion is preserved as it was. New answers, comments, and votes are closed.