-- Function to handle player character added

local function onCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid")
	local previousPosition = humanoid.RootPart.Position
	local isFalling = false

	-- Listen for character state changes
	humanoid.StateChanged:Connect(function(oldState, newState)
		if newState == Enum.HumanoidStateType.Freefall then
			isFalling = true
			previousPosition = humanoid.RootPart.Position
		elseif oldState == Enum.HumanoidStateType.Freefall and newState ~= Enum.HumanoidStateType.Freefall then
			if isFalling then
				isFalling = false
				handlePlayerFalling(game.Players:GetPlayerFromCharacter(character), humanoid.RootPart.Position, previousPosition)
			end
		end
	end)
end
