Help with detecting if the player is in the air or not

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
  • I made a cheap but reliable ability/skill system. I want it so that the player can only use abilities when grounded (with some exceptions to certain abilities)
  1. What is the issue? Include screenshots / videos if possible!
  • I feel like Ive done everything I could try. I tried checking the HumanoidStateType, but Im not sure what Im doing wrong. No matter what I tried, the player could still fire the event for the ability in the air.
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
  • Stated above. If you need me to post the codes I tried after this one then let me know.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

First Attempt

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer
local hum = plr.CharacterAdded:Wait():FindFirstChild("Humanoid")

local abilityCall = RS.Remotes.AbilityRemotes

local trigger = "E"
local cooldown = 1
local db = false

UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	
	if input.KeyCode == Enum.KeyCode[trigger] and db == false then
		if plr:GetAttribute("CharID") == 1 then
				db = true
				abilityCall.Tornado:FireServer()

				task.wait(cooldown)
				db = false
		end
	end
end)

Second Attempt

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer
local hum = plr.CharacterAdded:Wait():FindFirstChild("Humanoid")

local abilityCall = RS.Remotes.AbilityRemotes

local trigger = "E"
local cooldown = 1
local db = false

local function InAir()
	if hum then
		return hum:GetState() == Enum.HumanoidStateType.Freefall or hum:GetState() == Enum.HumanoidStateType.Jumping
	end
	return false
end

hum.StateChanged:Connect(function(_, newState)
	if newState == Enum.HumanoidStateType.Freefall or newState == Enum.HumanoidStateType.Jumping then
		print("in the air")
    else
		print("not in the air")
	end
end)

-- Initial check
if InAir() then
	print("in the air")
else
	UIS.InputBegan:Connect(function(input, gpe)
		if gpe then return end

		if input.KeyCode == Enum.KeyCode[trigger] and db == false then
			if plr:GetAttribute("CharID") == 1 then
				if plr:GetState() ~= Enum.HumanoidStateType.Freefall or hum:GetState() ~= Enum.HumanoidStateType.Jumping then
					db = true
					abilityCall.Tornado:FireServer()

					task.wait(cooldown)
					db = false
				end
			end
		end
	end)
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

One thing i like doing for detecting if the player is in air is checking humanoid’s FloorMaterial property. If it’s nil or Enum.Material.Air then the player is in the air, else they aren’t!

2 Likes

whered you place this script ? also can you print the “hum” variable because sometimes plr.CharacterAdded:Wait() doesnt work for me so it could be returning nil

if not u could use Humanoid.FloorMaterial or use raycasting to detect if theres anything below the character

1 Like

The script is in starterplayerscripts but I got it to work in a different method. thank you though

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