Need help making my fall script better

So my fall script detects how long I’ve been falling in seconds and if it falls past that threshold it plays the animation, I want it to track how many studs I’ve fallen so its a more accurate way unless its better to use velocity, I dont know.

Below is the script:

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local isFalling = false

local fallAnimationID = "rbxassetid://14092180850"
local fallAnimation = Instance.new("Animation")
fallAnimation.AnimationId = fallAnimationID
local fallAnimTrack = humanoid:LoadAnimation(fallAnimation)

local fallDelay = 3
local timeInAir = 0

local function resetFallTimer()
	timeInAir = 0
	if isFalling then
		isFalling = false
		fallAnimTrack:Stop()
	end
end

local function updateFallTimer(dt)
	if isFalling then
		timeInAir = timeInAir + dt
		if timeInAir >= fallDelay then
			if not fallAnimTrack.IsPlaying then
				fallAnimTrack:Play()
			end
		end
	end
end

humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Freefall then
		if not isFalling then
			isFalling = true
		end
	elseif newState ~= Enum.HumanoidStateType.Freefall and isFalling then
		resetFallTimer()
	end
end)

game:GetService("RunService").Heartbeat:Connect(updateFallTimer)

If anyone could help me do this because I’m have zero clue as to how I would approach this.

Well you can use the position property to find out the exact studs.

local initialFallPos = 0;
humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Freefall then
        initialFallPos = character.PrimaryPart.Position.Y
		if not isFalling then
			isFalling = true
		end
	elseif newState == Enum.HumanoidStateType.Landed and isFalling then
        if (character.PrimaryPart.Position.Y < initialFallPos) then
           local fallStuds = (initialFallPos - character.PrimaryPart.Position.Y) -- studs the player has fallen for
        end
		resetFallTimer()
	end
end)

Also I changed the ~= Enum.HumanoidStateType.Freefall to == Enum.HumanoidStateType.Landed, made more sense to me but you can revert it it shouldn’t really matter

1 Like