Detect for how long player has been falling

So I made a script that prints out how much time the player had been free falling. but I want the script to detect if the Airtime has been more than 0.5 seconds so a wind sound can start playing. But I don’t know how to make it detect if more than 0.5 seconds have passed while falling. This is the script:

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

local StartTime 

Humanoid.StateChanged:Connect(function(_, State)
	if State == Enum.HumanoidStateType.Freefall then 
		StartTime = tick()
	elseif State == Enum.HumanoidStateType.Landed then 
		local AirTime = tick() - StartTime 
		print("Was in air for "..AirTime.."s")
	end
end)
1 Like
 local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://”
 if AirTime == 0.5 then
sound:Play()

Not sure if it’ll work but you can try it.

So I did this:

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

local StartTime 

Humanoid.StateChanged:Connect(function(_, State)
	if State == Enum.HumanoidStateType.Freefall then 
		StartTime = tick()
	elseif State == Enum.HumanoidStateType.Landed then 
		local AirTime = tick() - StartTime 
		print("Was in air for "..AirTime.."s")
	else if State == Enum.HumanoidStateType.Freefall then
			local sound = Instance.new("Sound", game.Workspace)
				sound.SoundId = "rbxassetid://6960193959"
				if AirTime == 0.5 then
				sound:Play()
			end
		end 
	end
end)

But it errors saying Unknown Global AirTime

Change the value for “if AirTime” to 1, then check.

Nope still gives the error. The error is that it doesnt find the AirTime. Not the number value

Your airtime variable is a local inside of the if statement right now. It will not work outside of that. So make the variable earlier in the script

1 Like

When you jump something in the humanoid gets enabled, I’m not on computer so I’d wouldn’t be able to check although you can.

Test the game, go to your humanoid and jump and see what enables.

Free falling enables. Thats why The freefalling statement is there

How? SInce its already in the front of AirTime

Or you may be able to just change it to

That wouldnt work cuz I need to detect how much time or the sound will play even when the player just normal jumps

You are not waiting .5 seconds the script ends quicker than when you check if its .5 seconds. You should use wait() or delay() i’m not sure which is better in this case.

Right, but it’s just 0.5 seconds, when they jump it’ll past that very quick so it’ll be better to play the sound automatically after jumping, no?

Or you can just add this variable

 local AirTime = tick() - StartTime 

above the sound variable?

It kinda works but when the player lands it stops for a second then starts again. Its better to detect for how long.

I did this:

local Humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://6960193959"
sound.Volume = 3
local StartTime 

Humanoid.StateChanged:Connect(function(_, State)
	if State == Enum.HumanoidStateType.Freefall then 
		StartTime = tick()
		local AirTime = tick() - StartTime 
		if AirTime >= 0.5 then
			sound:Play()
		end
	elseif State == Enum.HumanoidStateType.Landed then 
		local AirTime = tick() - StartTime 
		print("Was in air for "..AirTime.."s")
		sound:Stop()
	end
end)

but it doesn’t play the sound. even if I’ve been falling for more than 0.5 seconds

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")

local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://6960193959"
sound.Volume = 3

local StartTime 

Humanoid.StateChanged:Connect(function(OldState,NewState)
	if NewState == Enum.HumanoidStateType.Freefall then
		StartTime = tick()
		delay(.5, function()
			if tick() - StartTime >= .5 and Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
				sound:Play()
			end
		end)
	elseif Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
		sound:Stop()
	end
end)
2 Likes
-- local script

local ps = game:GetService("Players")
local lp = ps.LocalPlayer

local TimeInAir = 0

while wait() do
  local ch = lp.Character
  if ch then 
   local c = RaycastParams.new()
   c.FilterType = Enum.RaycastFilterType.Blacklist
   c.FilterDescendantsInstances({ch})
   local r = workspace:Raycast(ch.HumanoidRootPart.Position,Vector3.new(0,-10,0),c)
   if not r.Instance then
   TimeInAir = TimeInAir+1
    -- There is nothing underneath, assume in air.
   else
   TimeInAir = 0
    -- Not in air.
   end
  end
1 Like
local Humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
local lastFall = 0

local sound = Instance.new("Sound", game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart"))
sound.SoundId = "rbxassetid://6960193959"
sound.Volume = 3

Humanoid.StateChanged:Connect(function(_, State)
	if State == Enum.HumanoidStateType.Freefall then 
		lastFall += 1
		local myLast = lastFall
		
		delay(0.5, function()
			if myLast == lastFall then
				sound:Play()
			end
		end)
	elseif State == Enum.HumanoidStateType.Landed then 
		sound:Stop()
	end
end)
1 Like

If you want to be fancy and want the sound to fade in :stuck_out_tongue:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")

local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://6960193959"
local Volume = 3

local StartTime

Humanoid.StateChanged:Connect(function(OldState,NewState)
	if NewState == Enum.HumanoidStateType.Freefall then
		StartTime = tick()
		delay(.5, function()
			if tick() - StartTime >= .5 and Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
				sound.Volume = 0
				sound:Play()
				for count = 1,10 do
					local Incr = Volume/10
					sound.Volume = sound.Volume + Incr
					wait(0.1)
				end
			end
		end)
	elseif Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
		sound:Stop()
	end
end)
1 Like

Thank God! I was trying to script a Tween to make it fade in. Thank you! Life saver

1 Like