function findNearestTorso(pos)
	local list = Game.Workspace:GetChildren()
	local torso = nil
	local dist = script.Parent.SETTINGS.ChaseRange.Value
	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") or temp2:FindFirstChild("Human1")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and (script.Parent.Torso.Position - OrigPos).magnitude <= script.Parent.SETTINGS.ChaseDistance.Value 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.2)
	--Chase
	local target = findNearestTorso(script.Parent.Torso.Position)
	if target ~= nil then
		script.Parent.Human:MoveTo(target.Position, target)
		script.Parent.Chasing.Value = true
		else
		script.Parent.Chasing.Value = false
	end
	
	if script.Parent.Human.Mana.Value < script.Parent.SETTINGS.Mana.Value then
		wait(.2)
		script.Parent.Human.Mana.Value = script.Parent.Human.Mana.Value + (script.Parent.SETTINGS.Mana.Value/100)
	end
	--Wander
	if TicksUntilWander > 0 then
		TicksUntilWander = TicksUntilWander - 1
	else		
		TicksUntilWander = math.random(10, 25)
		if target == nil then
			if script.Parent:FindFirstChild("Model") ~= nil then
				script.Parent.Human:MoveTo(Vector3.new(OrigPos.X + math.random(-script.Parent.SETTINGS.WanderRadius.Value, script.Parent.SETTINGS.WanderRadius.Value), OrigPos.Y, OrigPos.Z + math.random(-script.Parent.SETTINGS.WanderRadius.Value, script.Parent.SETTINGS.WanderRadius.Value)), script.Parent.Model:FindFirstChild("Part"))
			end
		end
	end
end

2 comments

0

Can you explain the question in more detail.