My friend and I were testing this AI for a game we're making, but since he was the person who got in his range first, the AI would only target him, but then even after my friend respawns, the AI doesn't target me, even though I'm in his range.
EDIT: I have removed the script because it was answered. My problem was I was returning nil too early.
You're returning nil too early in your findNearestTorso function. When the first player joins the server, that will be the only player targeted. If that player is not within 25 studs of the AI, the function will return nil not checking the other players.
Return nil after the for loop has finished. That way all players are processed, and the AI may confirm no one is within range.
function findNearestTorso() for _, plr in pairs(game.Players:GetChildren()) do while not plr.Character do wait() end if plr.Character:findFirstChild("Humanoid") then if (trooper.Torso.Position - plr.Character.Torso.Position).magnitude <= 25 then return plr.Character.Torso --else end end end return nil --Just move return nil to here. It's clear no one is within range at this point. end