local hum = script.Parent:WaitForChild("Humanoid") local root = script.Parent:WaitForChild("HumanoidRootPart") local spawnpos = script.Parent:WaitForChild("SpawnPosition")
local visionrange = 99999 local attackrange = 3 local abandonrange = 999 local abandonrangefromhome = 999
local target = nil
while wait(1) do
if target then
local plrRoot = target.HumanoidRootPart
local distance = (root.Position - plrRoot.Position).magnitude
local distancefromhome = (spawnpos.value - plrRoot.Position).magnitude
hum:MoveTo(plrRoot.Position - CFrame.new(root.Position, plrRoot.Position).LookVector * attackrange)
if distance <= attackrange + 2 then
script.AttackRemote:fire(plrRoot)
end
if distance > abandonrange then
print("abandoning target")
target = nil
hum:MoveTo(spawnpos.value)
end
if distancefromhome > abandonrangefromhome then
print("abandoning target")
target = nil
hum:moveto(spawnpos.value)
end
else
for i,v in pairs(game.Players:GetChildren()) do
if not game.Workspace:FindFirstChild(v.name) then continue end
local char = game.Workspace[v.name]
local plrRoot = char.HumanoidRootPart
local distance = (root.Position - plrRoot.Position).magnitude
local distancefromhome = (spawnpos.value - plrRoot.Position).magnitude
if distance < visionrange and distancefromhome then distancefromhome < abandonrangefromhome then
print("targeting"..v.name)
target = char
end
end
end
end

2 comments
You have 2 then. Replace the first one with and
^On line 36