How can I check how long the player has been falling?

  1. What do you want to achieve? A script that plays an animation if a player has been falling for 2 or more seconds
  2. What is the issue? I’m not sure where to add the code or how to implement wait() into the code
  3. What solutions have you tried so far? tried an if loop but it didn’t do anything

here’s my code

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


local stateConnection = Humanoid.StateChanged:Connect(function(oldState, newState)
	if (oldState == Enum.HumanoidStateType.Freefall) and (newState == Enum.HumanoidStateType.Landed) then
		local animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Animation)
		print("landed")
		Humanoid.WalkSpeed = 0
		wait(0.05)
		animation:Play()
		wait(1)
		Humanoid.WalkSpeed = 16
	end
end)

all help is appreciated

You can try this out and see if it works.

local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local start ;  
local fallTime ; 
if (Character) then
	
	if (Character:FindFirstChild("Humanoid")) or (Character:WaitForChild("Humanoid")) then
		
		local Humanoid = Character.Humanoid
		Humanoid.StateChanged:Connect(function(oldState,newState)
			print("State changed")
			if (newState) == Enum.HumanoidStateType.Freefall then
				print("falling")
				start = tick()
			elseif (newState == Enum.HumanoidStateType.Landed) then
				if (start) then
					print("Landed")
					fallTime = tick() - start 
					print(fallTime)
					start = nil 
					fallTime = nil 
				end 
			end 
		end)
	end 
end

Also note - I wrote this code in sublime text and thus if there are any mistakes such as missing an end or any syntax-tual mistake pardon.

And also I have not handled the animation or anything in this code.
Just add a conditional and play it depending on free Fall time.

3 Likes

After messing around with this for a bit I was able to get it to work! Thank you so much