My NPC Attack helicopter says it firing at me but Im not being damaged

(Game is r15)

The code:

local npc = {}
npc.stop = function()
	game.Workspace.BaseRaid.Value = false
	game.Workspace:FindFirstChild("BaseRaidHelicopter"):Destroy()
end
npc.run = function()
	game.Workspace.BaseRaid.Value = true
	-- Varaibles
	local Helicopter = script.Helicopter:Clone()
	local HelicopterPosition = Helicopter.Pos.Position
	local range = 600
	local damage = 35
	-- Run
	Helicopter.Parent = game.Workspace
	Helicopter.Helicopter:Play()
	Helicopter.Name = "BaseRaidHelicopter"
	-- Functions
	local function rocketDestroy(rocket)
		wait(4)
		rocket:Destroy()
	end
	local function fireRocket(player)
		local rocket = script.Rocket:Clone()
		rocket.Parent = game.Workspace
		rocket.CFrame = CFrame.new(HelicopterPosition, player.Character.HumanoidRootPart.Position)
		local bodyVelocity = Instance.new("BodyVelocity", rocket)
		bodyVelocity.Velocity = CFrame.new(rocket.Position, player.Character.HumanoidRootPart.Position).LookVector * 100
		rocket.RocketTrail.Enabled = true
		rocket.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				rocket.ExplosionSound:Play()
				if game.Players:GetPlayerFromCharacter(hit.Parent) then
					local explosion = Instance.new("Explosion", workspace)
					explosion.Position = hit.Parent.HumanoidRootPart.POsition
					rocket.BodyHit:Play()
					hit.Parent.Humanoid:TakeDamage(damage)
					rocketDestroy(rocket)
					wait(3)
					explosion:Destroy()
				end
				wait(3)
				rocketDestroy(rocket)
			end
		end)
	end
	local function findHumanoid()
		for i,v in pairs(game.Players:GetPlayers()) do
			if (v.Character.HumanoidRootPart.Position - HelicopterPosition).magnitude <= range then
				print(v.Name .." is in range of the helicopter, firing.")
			    fireRocket(v)
			end
		end
	end
	while wait(5) do
		findHumanoid()
	end
end
return npc

Well, the helicopter might say it’s firing but are you achieving the if statements conditions? The code seems to be fine, although I’m curious about this result:

if game.Players:GetPlayerFromCharacter(hit.Parent) then

Is it able to get the player from the character? I don’t see what’s wrong, maybe store the humanoid in a variable as well it’s good practice to keep what the function finds and not throw it away.

local humanoidHit = hit.Parent:FindFirstChild("Humanoid") 
if humanoidHit then
---other stuff
humanoidHit:TakeDamage(damage)
end

Edit: Also is the rocket able to hit anything? I have heard the .Touched event may be unreliable and so you would have to rely on other methods like raycasting or region3 maybe.

Alright damage and stuff works, it just now the helicopter will NOT look at me when it fires, plus the missiles are going a direction away from me.

Code

local npc = {}
npc.stop = function()
	game.Workspace.BaseRaid.Value = false
	game.Workspace:FindFirstChild("BaseRaidHelicopter"):Destroy()
end
npc.run = function()
	game.Workspace.BaseRaid.Value = true
	-- Varaibles
	local Helicopter = script.Helicopter:Clone()
	local HelicopterPosition = Helicopter.Pos.Position
	local debris = game:GetService("Debris")
	local range = 600
	local damage = 35
	-- Run
	Helicopter.Parent = game.Workspace
	Helicopter.Helicopter:Play()
	Helicopter.Name = "BaseRaidHelicopter"
	-- Functions
	local function rocketDestroy(rocket)
		wait(4)
		rocket:Destroy()
	end
	local function fireRocket(player)
		local rocket = script.Rocket:Clone()
		rocket.Parent = game.Workspace
		--rocket.Position = HelicopterPosition -- .CFrame = CFrame.new(HelicopterPosition, player.Character.HumanoidRootPart.Position)
		local bodyVelocity = Instance.new("BodyVelocity", rocket)
		bodyVelocity.MaxForce = Vector3.new(1000,1000,1000)
		Helicopter.Pos.CFrame = CFrame.lookAt(player.Character.HumanoidRootPart.Position, player.Character.HumanoidRootPart.Position)
		bodyVelocity.Velocity = CFrame.new(player.Character.HumanoidRootPart.Position, player.Character.HumanoidRootPart.Position).LookVector * 200 -- CFrame.new(HelicopterPosition, player.Character.HumanoidRootPart.Position).LookVector * 100
		rocket.RocketTrail.Enabled = true
		rocket.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				rocket.ExplosionSound:Play()
				if game.Players:GetPlayerFromCharacter(hit.Parent) then
					local explosion = Instance.new("Explosion", workspace)
					explosion.Position = hit.Parent.HumanoidRootPart.Position
					explosion.ExplosionType	= Enum.ExplosionType.NoCraters
					rocket.BodyHit:Play()
					hit.Parent.Humanoid:TakeDamage(damage)
					debris:AddItem(rocket, 5)
					debris:AddItem(explosion, 3)
				end
			end
		end)
	end
	local function findHumanoid()
		for i,v in pairs(game.Players:GetPlayers()) do
			if (v.Character.HumanoidRootPart.Position - HelicopterPosition).magnitude <= range then
				print(v.Name .." is in range of the helicopter, firing.")
			    fireRocket(v)
			end
		end
	end
	while wait(5) do
		findHumanoid()
	end
end
return npc

The If Character part to see if there a player or NPC

Maybe check the chopper’s orientation before it fires and just make it turn when it fires. And with the checking humanoid, you should check when it fires not before it fires to ensure the player’s position. I don’t really see why the it’s not working though. I’m the guy on stream btw.

1 Like

Well, then that is a CFrame problem, btw I recommend using variables to avoid this length piece of code.

Helicopter.Pos.CFrame = CFrame.lookAt(player.Character.HumanoidRootPart.Position, player.Character.HumanoidRootPart.Position)

The solution is to make it look it the correct direction which is determined the same way you did the magnitude calculation:

local lookToEnemyVector = v.Character.HumanoidRootPart.Position - HelicopterPosition

This should be the resultant code since you define the character the helicopter position towards the player position. Please refer to the CFrame API reference for guidance with how to construct the proper CFrame.

CFrame.new ( Vector3 pos, Vector3 lookAt )
Creates a new CFrame located at pos and facing towards lookAt , assuming that (0, 1, 0) is considered “up”.

local playerPos = player.Character.HumanoidRootPart.Position
Helicopter.Pos.CFrame = CFrame.lookAt(HelicopterPosition,playerPos)
1 Like

make a loop when it fires for the following:
either if it’s model use Model:SetPrimaryPartCFrame

Helicopter.CFrame = CFrame.New(Helicopter.Position--[[position]], character.HumanoidRootPart --[[look at pos]])