NPC that Shoots | Help/Problem

I am trying to make an NPC that shoots at incoming enemy NPCs

However, the script I have only kills/shoots the enemies they see when they spawn in, so if you make them move to a closer position where it would be able to shoot, it still will not do anything

btw I am not a good scripter so I may not know certain scripting terms

local char = script.Parent
local myHrp = char:WaitForChild("HumanoidRootPart")
local myHum = char:WaitForChild("Humanoid")
local orgin = myHrp.Position
local params = RaycastParams.new()
local distOfInt = 100
local att1 = char.Tool.Hole.Att1
local rnd = Random.new()
local loopPauses = rnd:NextInteger(25, 35)
local loopCnt = 0
local snd = char.Tool.Hole.Sound
local damage = 50
local Fire123 = myHum:WaitForChild("Animator"):LoadAnimation(script.FireAnim)


local function fire(eHum)
	snd:Play()
	Fire123.Priority = Enum.AnimationPriority.Action
	Fire123:Play()
	eHum:TakeDamage(damage)
end


local function findNearestHrp(dist)
	local closestHrp = nil
	for i, v in pairs(workspace.Enemies:GetChildren()) do
		local hrp = v:FindFirstChild("HumanoidRootPart")
		if hrp then
			local tmpDis = (hrp.Position - orgin).Magnitude
			local hum = v:FindFirstChild("Humanoid")
			if tmpDis < dist and hum and hum.Health > 0 then
				dist = tmpDis
				closestHrp = hrp
			end
		end
	end
	return closestHrp
end

local function target(eHrp)
	local stop = (eHrp.Position-orgin).Unit * distOfInt
	params.FilterDescendantsInstances = { char }
	params.FilterType = Enum.RaycastFilterType.Blacklist
	local result = workspace:Raycast(orgin, stop, params)
	if result and result.Instance.Parent and 
		result.Instance.Parent:FindFirstChild("Humanoid") then
		att1.WorldPosition = result.Position
		if loopCnt > loopPauses then
			fire(eHrp.Parent.Humanoid)
			loopCnt = 0
		end
		loopCnt += 1
	end
end

while wait() do
	if myHum.Health < 1 then
		att1.Position = Vector3.new(0, 0, 0)
		break
	end
	local closestHrp = findNearestHrp(distOfInt)
	if closestHrp then
		target(closestHrp)
	else
		att1.Position = Vector3.new(0, 0, 0)
	end
end

Do I fix this by just updating a position constantly? or what am I supposed to do please help.

1 Like

reply to get to top sinse this is still and issue

replying again since I have not been able to fix this