I’m currently fixing up the AI from my game and while testing this happend with the enemys:
This is the player finding code:
local function FindTarget()
local Distance = nil
local nearestTarget = nil
local MaxDistance = EnemyHumanoid.MaxVisionDistance.Value
for _, Player in pairs(Players:GetPlayers()) do
if Player.Character and Player.Character.HumanoidRootPart then
local Target = Player.Character
Distance = (Enemy.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude
if Distance <= MaxDistance and Player.Character.Humanoid.Health > 0 then
nearestTarget = Target
MaxDistance = Distance
else
nearestTarget = nil
end
end
end
return nearestTarget
end