I see a lot of loops indeed, could you use coroutine if possible?
Possibly, would it be better just to rewrite this code with coroutine?
If you wanna. I would recommend avoiding as many loops because from what’ve seen you call a for loop on every while true. Even better try to organize your workspace so you don’t have to use workspace:GetChildren()
, and if you wanna get players just use this.
local function FindTarget()
local dist = Info.RangeofVision
local found = nil
for _,v in game:GetService("Players"):GetPlayers() do
local Victim = v.Character
if Victim and Victim:FindFirstChild("HumanoidRootPart") and Victim:FindFirstChildOfClass("Humanoid").Health > 0 and Victim:FindFirstChild("_enemytag") == nil and Victim ~= Character and (Character.HumanoidRootPart.Position - Victim.HumanoidRootPart.Position).Magnitude <= dist then
dist = (Character.HumanoidRootPart.Position - Victim.HumanoidRootPart.Position).Magnitude
found = Victim
end
end
target = found
end
1 Like