local db = false

-- Personal creep stats --

local slashanim = script.slashanim
local atkspeed = 70/65
local dps = math.random(19, 23)

--------------------------

local PathfindingService = game:GetService("PathfindingService")
local CreepAttackRange = 25
local TargetCreepListDire = {}
local TargetCreepListRadiant = {}
local Waypoints = {}
local DefaultWalkSpeed = 325 / 25
local Fighting = false
local Humanoid = script.Parent:WaitForChild("Humanoid")
local HumanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
Humanoid.WalkSpeed = DefaultWalkSpeed
script:WaitForChild("ReachedPos")

local function attackCreep(creep, enemycreep)
	if enemycreep.Humanoid.Health > 0 and creep.Humanoid.Health > 0 then
		fighting = true
		waypoints = {}
		creep.Humanoid:MoveTo(enemycreep.HumanoidRootPart.Position)
		if (creep.HumanoidRootPart.Position - enemycreep.HumanoidRootPart.Position).Magnitude <= 3.5 then
			creep.Humanoid.WalkSpeed = DefaultWalkSpeed / 2.5
			if db == false then
				db = true
				creep.Head.Slash:Play()
				if slashanim.Value == 1 then
					creep.Humanoid:LoadAnimation(script.Slash1):Play()
					slashanim.Value = 2
				elseif slashanim.Value == 2 then
					creep.Humanoid:LoadAnimation(script.Slash2):Play()
					slashanim.Value = 1
				end
				if math.random(0,enemycreep.MissChance.Value) <= enemycreep.MissChance.Value / 25 then
					if enemycreep:FindFirstChild("invincibility").Value == false then
						enemycreep.Humanoid:TakeDamage(dps)
					end
					wait(atkspeed)
					db = false
				end
			end
		else
			creep.Humanoid.WalkSpeed = DefaultWalkSpeed
		end
		if enemycreep.Humanoid.Health <= 0 then
			local cs = script:Clone()
			cs.Parent = script.Parent
			for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
				if v.Name == "Slash1" or v.Name == "Slash2" then
					v:Stop()
				end
			end
			script:Destroy()
		end
	else
		local cs = script:Clone()
		cs.Parent = script.Parent
		for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
			if v.Name == "Slash1" or v.Name == "Slash2" then
				v:Stop()
			end
		end
		script:Destroy()
	end
end

local function attackTower(creep, tower)
	if tower.Faction.Value ~= creep.Faction.Value then
		if tower.Health.Value > 0 and creep.Humanoid.Health > 0 then
			fighting = true
			waypoints = {}
			creep.Humanoid:MoveTo(tower.BasePart.Position)
			if (creep.HumanoidRootPart.Position - tower.BasePart.Position).Magnitude <= 7 then
				creep.Humanoid.WalkSpeed = DefaultWalkSpeed / 2.5
				if db == false then
					db = true
					creep.Head.Slash:Play()
					if slashanim.Value == 1 then
						creep.Humanoid:LoadAnimation(script.Slash1):Play()
						slashanim.Value = 2
					elseif slashanim.Value == 2 then
						creep.Humanoid:LoadAnimation(script.Slash2):Play()
						slashanim.Value = 1
					end
					if tower:FindFirstChild("invincibility").Value == false then
						tower.Health.Value = tower.Health.Value - (dps / 1.2)
					end
					wait(atkspeed)
					db = false
				end

			else
				creep.Humanoid.WalkSpeed = DefaultWalkSpeed
			end
			if tower.Health.Value <= 0 then
				local cs = script:Clone()
				cs.Parent = script.Parent
				for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
					if v.Name == "Slash1" or v.Name == "Slash2" then
						v:Stop()
					end
				end
				script:Destroy()
			end
		else
			return
		end
	end
end

local function Lane(creep, targetCreepList, enemyThrone, enemyTowers)
	while creep.Humanoid.Health > 0 and fighting == false do
		if fighting == false then
			if creep.Faction.Value == "Radiant" then
				if script.ReachedPos.Value == 0 then
					creep.Humanoid.WalkSpeed = DefaultWalkSpeed
					local path = PathfindingService:CreatePath()
					path:ComputeAsync(creep.PrimaryPart.Position, game.Workspace.Top2.Position)

					waypoints = path:GetWaypoints()

					for i, waypoint in ipairs(waypoints) do
						creep.Humanoid:MoveTo(waypoint.Position)
						creep.Humanoid.MoveToFinished:Wait()
					end
					if (creep.PrimaryPart.Position - game.Workspace.Top2.Position).Magnitude <= 10 then
						script.ReachedPos.Value = 1
						local cs = script:Clone()
						cs.Parent = script.Parent
						for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
							if v.Name == "Slash1" or v.Name == "Slash2" then
								v:Stop()
							end
						end
						script:Destroy()
					end
					wait()
				elseif script.ReachedPos.Value == 1 then
					creep.Humanoid.WalkSpeed = DefaultWalkSpeed
					local path = PathfindingService:CreatePath()
					path:ComputeAsync(creep.PrimaryPart.Position, game.Workspace.Bot2.Position)

					waypoints = path:GetWaypoints()

					for i, waypoint in ipairs(waypoints) do
						creep.Humanoid:MoveTo(waypoint.Position)
						creep.Humanoid.MoveToFinished:Wait()
					end
					if (creep.PrimaryPart.Position - game.Workspace.Bot2.Position).Magnitude <= 10 then
						script.ReachedPos.Value = 2
						local cs = script:Clone()
						cs.Parent = script.Parent
						for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
							if v.Name == "Slash1" or v.Name == "Slash2" then
								v:Stop()
							end
						end
						script:Destroy()
					end
					wait()
				elseif script.ReachedPos.Value == 2 then
					creep.Humanoid.WalkSpeed = DefaultWalkSpeed
					local path = PathfindingService:CreatePath()
					path:ComputeAsync(creep.PrimaryPart.Position, enemyThrone.Position)

					waypoints = path:GetWaypoints()

					for i, waypoint in ipairs(waypoints) do
						creep.Humanoid:MoveTo(waypoint.Position)
						creep.Humanoid.MoveToFinished:Wait()
					end
					wait()
				end
			elseif creep.Faction.Value == "Dire" then
				if script.ReachedPos.Value == 0 then
					creep.Humanoid.WalkSpeed = DefaultWalkSpeed
					local path = PathfindingService:CreatePath()
					path:ComputeAsync(creep.PrimaryPart.Position, game.Workspace.Bot2.Position)

					waypoints = path:GetWaypoints()

					for i, waypoint in ipairs(waypoints) do
						creep.Humanoid:MoveTo(waypoint.Position)
						creep.Humanoid.MoveToFinished:Wait()
					end
					if (creep.PrimaryPart.Position - game.Workspace.Bot2.Position).Magnitude <= 10 then
						script.ReachedPos.Value = 1
						local cs = script:Clone()
						cs.Parent = script.Parent
						for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
							if v.Name == "Slash1" or v.Name == "Slash2" then
								v:Stop()
							end
						end
						script:Destroy()
					end
					wait()
				elseif script.ReachedPos.Value == 1 then
					creep.Humanoid.WalkSpeed = DefaultWalkSpeed
					local path = PathfindingService:CreatePath()
					path:ComputeAsync(creep.PrimaryPart.Position, game.Workspace.Top2.Position)

					waypoints = path:GetWaypoints()

					for i, waypoint in ipairs(waypoints) do
						creep.Humanoid:MoveTo(waypoint.Position)
						creep.Humanoid.MoveToFinished:Wait()
					end
					if (creep.PrimaryPart.Position - game.Workspace.Top2.Position).Magnitude <= 10 then
						script.ReachedPos.Value = 2
						local cs = script:Clone()
						cs.Parent = script.Parent
						for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
							if v.Name == "Slash1" or v.Name == "Slash2" then
								v:Stop()
							end
						end
						script:Destroy()
					end
					wait()
				elseif script.ReachedPos.Value == 2 then
					creep.Humanoid.WalkSpeed = DefaultWalkSpeed
					local path = PathfindingService:CreatePath()
					path:ComputeAsync(creep.PrimaryPart.Position, enemyThrone.Position)

					waypoints = path:GetWaypoints()

					for i, waypoint in ipairs(waypoints) do
						creep.Humanoid:MoveTo(waypoint.Position)
						creep.Humanoid.MoveToFinished:Wait()
					end
					wait()
				end
			end
		else
			waypoints = {}
		end
	end
end

wait(0.1)
local function FindNearestTorso(targetList, position)
	local minDistance = math.huge
	local nearestTorso = nil

	for i, enemy in ipairs(targetList) do
		local enemyTorso = enemy:WaitForChild("HumanoidRootPart")
		local distance = (position - enemyTorso.Position).Magnitude

		if distance < minDistance then
			minDistance = distance
			nearestTorso = enemyTorso
		end
	end

	return nearestTorso
end

local function NearestTower(position, faction)
	local minDistance = math.huge
	local nearestTower = nil

	local towers = game.Workspace:GetChildren()
	for i, tower in ipairs(towers) do
		if tower.Name:match("Tower%d+") and tower.Faction.Value ~= faction and tower.Health.Value > 0 then
			local towerPosition = tower:WaitForChild("BasePart").Position
			local distance = (position - towerPosition).Magnitude

			if distance < minDistance then
				minDistance = distance
				nearestTower = tower:WaitForChild("BasePart")
			end
		end
	end

	return nearestTower
end

local enemyTowersRadiant = {}
local enemyTowersDire = {}

for i, enemyCreep in pairs(workspace:GetChildren()) do
	if enemyCreep.Name == "DireCreep" then
		table.insert(TargetCreepListRadiant, enemyCreep)
		coroutine.wrap(function()
			while wait() do
				local nearestTorso = FindNearestTorso(script.Parent.HumanoidRootPart.Position)
				if nearestTorso and (script.Parent.HumanoidRootPart.Position - nearestTorso.Position).Magnitude < CreepAttackRange and nearestTorso.Parent.Faction.Value ~= script.Parent.Faction.Value then
					attackCreep(script.Parent, nearestTorso.Parent)
				end
				local nearestTower = NearestTower(script.Parent.HumanoidRootPart.Position, "Dire")
				if nearestTower and (script.Parent.HumanoidRootPart.Position - nearestTower.Position).Magnitude < CreepAttackRange * 1.9 then
					fighting = true
					attackTower(script.Parent, nearestTower.Parent)
				end
			end
		end)()
	elseif enemyCreep.Name == "RadiantCreep" then
		table.insert(TargetCreepListDire, enemyCreep)
		coroutine.wrap(function()
			while wait() do
				local nearestTorso = FindNearestTorso(script.Parent.HumanoidRootPart.Position)
				if nearestTorso and (script.Parent.HumanoidRootPart.Position - nearestTorso.Position).Magnitude < CreepAttackRange and nearestTorso.Parent.Faction.Value ~= script.Parent.Faction.Value then
					attackCreep(script.Parent, nearestTorso.Parent)
				end
				local nearestTower = NearestTower(script.Parent.HumanoidRootPart.Position, "Radiant")
				if nearestTower and (script.Parent.HumanoidRootPart.Position - nearestTower.Position).Magnitude < CreepAttackRange * 1.9 then
					fighting = true
					attackTower(script.Parent, nearestTower.Parent)
				end
			end
		end)()
	end
end

if script.Parent.Name == "DireCreep" then
	for _, creep in ipairs(TargetCreepListDire) do
		Lane(script.Parent, TargetCreepListDire, game.Workspace.RadiantAncient, enemyTowersRadiant)
	end
else
	for _, creep in ipairs(TargetCreepListRadiant) do
		Lane(script.Parent, TargetCreepListRadiant, game.Workspace.DireAncient, enemyTowersDire)
	end
end оптимизируй мой скрипт