How do you make AI follow specific players only

How do you make simple AI follow specific players only, I have 2 types of players:

  • Infected
  • None Infected

The AI should only follow the None Infected group of players, I can use booleans to define that instead of Playerteams. I only need a way to make the AI to not follow specific players and follow the rest.

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 1000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("Torso")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end
while true do
	wait(0.5)
	local target = findNearestTorso(script.Parent.Torso.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
	end
end

the ai follow script I borrowed in the toolbox

2 Likes

here’s a suggestion u can use Bool values named “Infected”, attributes or tables, but let’s say you use bool values then we’ll put them inside the player’s characters we can just(but it seems like you already know about this assuming from what you said?)

if temp2.Infected.Value == false then
--- Follow this guy
end

also u can just use for pairs loop.

Alternatively, if your game has no NPCs that it’ll be following around, you can use Players:GetPlayers() to retrieve their Characters and Team. Downside being if they die, the character object tends to mess up a bit. (though this is mainly apparent on LocalScripts)

sorry It took long because I was playing something else, but Is there a way to call the boolean if its in game.Players ?

1 Like

If it’s inside the Player instance itself, get the player’s name.
Use :FindFirstChild() to get the player, which’ll lead you to the value.

no I meant that temp2 is the workspace Character of the player, and since that the boolean is not inside the Character, if theres a way to call from game.Players

you can get the player instance through the character by game.Players:GetPlayerFromCharacter(Player.Character)

Yes, if it’s a regular script.