How to check if player is in the air for an air defense system

I lowered the cooldown to 0 temporarily, to see if it worked, but even when I did, the rockets never shot out

Thereā€™s not enough info right now for me to know what might be preventing it from working.

  • Does it ever warn ā€œActivating Rocketā€ in the Output?
  • Are there any errors appearing in the Output?

And lastly:

Hereā€™s the modified code:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

-- Settings

local airTimeThreshold = 0
local desiredTurretFireWaitTime = 0

local midairStates = {
	Enum.HumanoidStateType.Jumping,
	Enum.HumanoidStateType.Freefall,
	Enum.HumanoidStateType.Flying
}

local endStates = {
	Enum.HumanoidStateType.Landed,
	Enum.HumanoidStateType.Running,
	Enum.HumanoidStateType.Climbing
}

--

-- Main Functions

local currentPlayers = {}

local function disconnectConnections(Table)

	for _, connection in Table do
		if typeof(connection) == "RBXScriptConnection" then
			connection:Disconnect()
			connection = nil
		end
	end

end

local function activateRocket(Character)
	local function activateRocket(Character)
		warn("Activating Rocket")
		local minimumHeight = 13
		local range = 30

		local Humanoid = Character:WaitForChild("Humanoid")

		local characterCFrame = Character:GetPivot()
		local characterPosition = characterCFrame.Position

		local model = script.Parent-- Reference to the turret model here
		local primaryPart = model.PrimaryPart
		local primaryPartPosition = primaryPart.Position

		local heightThreshold = primaryPartPosition.Y + minimumHeight

		if characterPosition.Y < heightThreshold then warn("Condition1 Failed") return end

		local subtractedPositions = characterPosition - primaryPartPosition
		if math.abs(subtractedPositions.Magnitude) > range then warn("Condition2 Failed") return end

		local rocket = model.Rocket:Clone()
		local rotate = rocket.Rotate
		local move = rocket.Move

		rocket:SetAttribute("followPlayer", Character.Name)
		rocket.Parent = workspace

		rotate.Enabled = true
		move.Enabled = true

		rocket.rocketMesh.Transparency = 0
	end
end


local function onCharacterSpawn(player, Character)

	local playerTable = currentPlayers[player]
	local currentlyMidair = false

	local Humanoid = Character:WaitForChild("Humanoid")
	playerTable["StateChangedConnection"] = Humanoid.StateChanged:Connect(function(oldState, newState)

		if table.find(midairStates, newState) then

			if currentlyMidair == true then return end

			currentlyMidair = true
			local currentAirTime = 0

			local preparingToFire = false
			local currentWaitTime = 0

			playerTable["RunServiceConnection"] = RunService.Heartbeat:Connect(function(deltaTime)
				currentAirTime += deltaTime

				--print(currentAirTime)

				if currentAirTime >= airTimeThreshold then
					warn("alert!")

					if preparingToFire == false then
						preparingToFire = true
						currentWaitTime = currentAirTime
					end

					currentWaitTime += deltaTime
					if currentWaitTime >= desiredTurretFireWaitTime then
						currentWaitTime -= desiredTurretFireWaitTime
						activateRocket(Character)
					end
				end
			end)

		elseif table.find(endStates, newState) then
			disconnectConnections({playerTable["RunServiceConnection"]})
			currentlyMidair = false
			print("Disconnected RunService connection")
		end
	end)

end

local function onPlayerJoin(player)

	currentPlayers[player] = {}

	if player.Character then
		onCharacterSpawn(player, player.Character)
	end

	player.CharacterAdded:Connect(function(Character)
		onCharacterSpawn(player, Character)
	end)

	player.CharacterRemoving:Connect(function()	
		local playerTable = currentPlayers[player]
		disconnectConnections(playerTable)
	end)

end

for _, player in Players:GetPlayers() do
	task.spawn(onPlayerJoin, player)
end

Players.PlayerAdded:Connect(onPlayerJoin)

Players.PlayerRemoving:Connect(function(player)
	currentPlayers[player] = nil
end)
1 Like

A duplicate activateRocket function was created, which means that when itā€™s being called from the function connected to RunService.Heartbeat* (within the function activated from the Humanoid.StateChanged event), itā€™s only calling the outer-most function and not the one where the rocket-related code actually is. Thatā€™s why it was never warning ā€œActivating Rocketā€ in the Output.

To resolve this, make sure thereā€™s only one activateRocket function:

local function activateRocket(Character)
	warn("Activating Rocket")
	local minimumHeight = 13
	local range = 30

	local characterCFrame = Character:GetPivot()
	local characterPosition = characterCFrame.Position

    local model = script.Parent -- Reference to the turret model here
	local primaryPart = model.PrimaryPart
	local primaryPartPosition = primaryPart.Position
		
	local heightThreshold = primaryPartPosition.Y + minimumHeight
		
	if characterPosition.Y < heightThreshold then warn("Condition1 Failed") return end
		
	local subtractedPositions = characterPosition - primaryPartPosition
	if math.abs(subtractedPositions.Magnitude) > range then warn("Condition2 Failed") return end
		
	local rocket = model.Rocket:Clone()
	local rotate = rocket.Rotate
	local move = rocket.Move
		
	rocket:SetAttribute("followPlayer", Character.Name)
	rocket.Parent = workspace
		
	rotate.Enabled = true
	move.Enabled = true

	rocket.rocketMesh.Transparency = 0
end

It works! Thank you very much!

1 Like

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