How do I tell how long the player has been falling for?

I’m trying to make a skydiving system, but I don’t know how to make a LocalScript tell if the player has been falling for a certain amount of seconds.

For example, if the player has been falling for 1 second, and hasn’t touched the ground yet, then print, “Able to deploy”, or else just don’t print anything. Can anyone write me a short script please?

Use the Humanoid.StateChanged method to see when the player begins falling.

local character = script.Parent

local humanoid = character:WaitForChild("Humanoid")

local TimeInAir = nil

-- listen to humanoid state
local function onStateChanged(_oldState, newState)
	if newState == Enum.HumanoidStateType.FreeFalling then
		TimeInAir = tick()
	elseif newState == Enum.HumanoidStateType.Landed then
		TimeInAir = nil
	end
end

humanoid.StateChanged:Connect(onStateChanged)
while task.wait() do
     if TimeInAir then
          print(string.format("Falling for %.2f seconds",tick()-TimeInAir)
     end
end

I tried to adjust it, but it didn’t work. Did I do something wrong here?

local character = game.Players.LocalPlayer.Character

local humanoid = character:WaitForChild("Humanoid")

local TimeInAir = nil

-- listen to humanoid state
local function onStateChanged(_oldState, newState)
	if newState == Enum.HumanoidStateType.Freefall then
		TimeInAir = tick()
	elseif newState == Enum.HumanoidStateType.Landed then
		TimeInAir = nil
	end
end

humanoid.StateChanged:Connect(onStateChanged)
while task.wait() do
	if TimeInAir then
		print(string.format("Falling for %.2f seconds",tick()-TimeInAir))
		if TimeInAir >= 0.7 then
			print("DEPLOY")
		end
	end
end

You’d need to do

Tick()-TimeInAir

To get the time in seconds,.

Obviously since its an ongoing loop You’d probably need to add in some sort of denounce, but that’s up to you.

Nvm I got it, thx so much tho!

while task.wait() do
	if TimeInAir then
		print(string.format("Falling for %.2f seconds",tick()-TimeInAir))
		if (tick()-TimeInAir) >= 0.7 then
			print("HAHAHA")
		end
	end
end

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