Detect whether player is midair

Basically what’s in the title. I wish to create a sword system in which if the player clicks while they are midair, then they will do a sword smash to ground thingy. How would i detect if they are midair at the time of the click? If they’re jumping, falling etc.

thanks, for some reason it wouldn’t come up when i searched for it

Then close the post by marking my message as the message that solved the issue

first i need to test if the solutions jn that post works

1 Like

if it doesnt i can help with this.

heres an example code

local player = game.Players.LocalPlayer

local function isMidAir()
    local character = player.Character
    if not character then return false end

    local humanoid = character:FindFirstChild("Humanoid")
    if not humanoid then return false end

    -- check if the player is jumping or freefalling
    local state = humanoid:GetState()
    return state == Enum.HumanoidStateType.Jumping or state == Enum.HumanoidStateType.Freefall
end
1 Like

Didn’t know it was that simple

Hey dude, shouldnt he have closed this post?

Yeah I think so, idk why he didn’t

cause i haven’t had thr abikity to test it yet

how make an event trigger if they’ve stopped falling, or hit the ground

We can use BindableEvents for our case, then we can call the :Fire() method to the event, so we can handle the event trigger elsewhere in your codebase.

Assuming you have an explorer structure like so:

Where we have ‘LocalScript’, you can use the following code:

local PS: Players = game:GetService("Players")
local RS: ReplicatedStorage = game:GetService("ReplicatedStorage")

local BindableEventsFolder: Folder = RS:WaitForChild("BindableEvents", 3)

local IsMidAirEvent: BindableEvent = BindableEventsFolder:WaitForChild("IsMidAir", 3)
local IsGroundedEvent: BindableEvent = BindableEventsFolder:WaitForChild("IsGrounded", 3)

local character: Model = PS.LocalPlayer.Character or PS.LocalPlayer.CharacterAdded:Wait()
local humanoid: Humanoid = character:WaitForChild("Humanoid", 3)

local currentState: number = 0

--[[
	Essentially, we're grouping multiple states to a numeric identifier
        to avoid the event being triggered more than once, which are:
		
	- 1 = Mid-Air
	- 2 = Grounded
	
	Feel free to add more states to these numbers if needed.
]]
local StateMapperEnum = {
	MidAir = 1,
	Grounded = 2
}

local stateMapper: { [Enum.HumanoidStateType]: number } = {
	[Enum.HumanoidStateType.Jumping] = StateMapperEnum.MidAir,
	[Enum.HumanoidStateType.Freefall] = StateMapperEnum.MidAir,
	[Enum.HumanoidStateType.Running] = StateMapperEnum.Grounded,
	[Enum.HumanoidStateType.Landed] = StateMapperEnum.Grounded
}

humanoid.StateChanged:Connect(function(oldState: Enum.HumanoidStateType, newState: Enum.HumanoidStateType)
	if currentState ~= stateMapper[newState] then
		currentState = stateMapper[newState]

		if currentState == StateMapperEnum.MidAir then
			IsMidAirEvent:Fire()
		elseif currentState == StateMapperEnum.Grounded then
			IsGroundedEvent:Fire()
		end
	end
end)

-- Examples of them being used elsewhere, assuming you have access to the BindableEvent objects.
IsMidAirEvent.Event:Connect(function()
	print("Player is mid-air")
end)

IsGroundedEvent.Event:Connect(function()
	print("Player is grounded")
end)