Issue With NPC Targetting

Hello,
I have drafted a simple script that will make it so a zombie scouts for players, and when in the radius, attacks them. However, I have encountered an issue with it. For some reason, if there are more than 1 players, it will completely ignore one of the players, even if said player gets close, and keep patrolling, until the other player walks up into the radius, and attacks that player. I have done countless hours worth of troubleshooting and tweaking, yet, I still cannot figure out the issue.

This is not the full script, it is a snippet from a much larger module script in serverscriptservice.

	local function findtarget()
			local target = nil
			local distance = zombieStats.zombieStats[Name]["Range"]

			local partTable = game.Workspace:GetChildren()

			for i, v in ipairs(partTable) do
				if game.Players:GetPlayerFromCharacter(v) then
					print(v)
					
					local char = v
					local humanoid = char:FindFirstChild("Humanoid")
					local root = v:FindFirstChild("HumanoidRootPart")

					local mag = (root.Position - clone.Torso.Position).Magnitude

					if root and humanoid and humanoid.Health > 0 and mag <= distance then
						target = root
						
					elseif root and humanoid and humanoid.Health > 0 and mag >= distance then
						target = nil
						
					end
				end
			end

		coroutine.wrap(function()
			while true do
			
				if hum.Health > 0 then
					local target = findtarget()
					if target then
						hum:MoveTo(target.Position)
						print(target)
					else
						target = nil
						print(target)
						local pos
						pos = clone.Torso.Position * Vector3.new(math.random(-15 , 15),0,math.random(-15, 15))
						wait(0.1)
						hum:MoveTo(pos)
						wait(1)
					end
				end
			wait()
			end
			
		end)

Any help would be appreciated.

1 Like

Just by looking at it I can’t really find the error, if you like I can write a new version for it, that you can modify.

I’d appreciate it - and I’m not sure what the issue is either. I even tried putting it in a regular server script inside an NPC, and it still had the same issue. I’d appreciate any help.

Okay, I will write an example (not the best but should work, I did not add damage, if you want I can):

--!nocheck

function MoveToNearsetPlayer(mob: Model)
	local Players = game:GetService("Players")
	local AllPlayers = Players:GetPlayers()
	local MaxDistance = 20 --your max distance here (in studs)
	local Target = nil
	local TableTargets = {}
	
	task.wait(5) --A delay to let the game load

    mob.PrimaryPart:SetNetworkOwner(nil)
	
	game:GetService("RunService").Heartbeat:Connect(function()
		if Target ~= nil then 
			mob:FindFirstChildOfClass("Humanoid"):MoveTo(Target.Character.PrimaryPart.Position)

			Target.Character:FindFirstChildOfClass("Humanoid").Died:Connect(function()
				Target = nil
				
				table.remove(TableTargets, table.find(TableTargets, Target))
			end)
		end
		
		for index, value in ipairs(AllPlayers) do
			if table.find(TableTargets, value) == nil then
				table.insert(TableTargets, value)
				print(TableTargets)
			end
		end
		
		table.sort(TableTargets, function(a,b)
			return (mob.PrimaryPart.Position - a.Character.PrimaryPart.Position).Magnitude < (mob.PrimaryPart.Position - b.Character.PrimaryPart.Position).Magnitude
		end)
		
		if (mob.PrimaryPart.Position - TableTargets[1].Character.PrimaryPart.Position).Magnitude > MaxDistance then
			return
		end
		
		Target = TableTargets[1]
	end)
end

game.Players.PlayerAdded:Wait()

task.spawn(MoveToNearsetPlayer, workspace:FindFirstChild("mob",true))```

Thanks! Ill load it up soon and test it. Dont worry about the damage, I already have that part covered. Ill get back to you once I’ve tested it.

I managed to fix it! The line

elseif root and humanoid and humanoid.Health > 0 and mag >= distance then
						target = nil
						

was causing the issue the entire time. Thanks for the help with the script, though, I may consider using it.

1 Like