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 creepsattackrange = 25
local targetCreepListDire = {}
local targetCreepListRadiant = {}
local waypoints = {}
local dfws = 325 / 25
local fighting = false
script.Parent:WaitForChild("Humanoid")
script.Parent:WaitForChild("HumanoidRootPart")
script.Parent:WaitForChild("Humanoid").WalkSpeed = dfws
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 = dfws / 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 = dfws
		end
		if enemycreep.Humanoid.Health <= 0 then
			for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
				if v.Name == "Slash1" or v.Name == "Slash2" then
					v:Stop()
				end
			end
			creep.Humanoid.WalkSpeed = dfws
			local cs = script.Parent:Clone()
			cs.Parent = script.Parent.Parent
			script:Destroy()
		end
	else
		for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
			if v.Name == "Slash1" or v.Name == "Slash2" then
				v:Stop()
			end
		end
		creep.Humanoid.WalkSpeed = dfws
		local cs = script.Parent:Clone()
		cs.Parent = script.Parent.Parent
		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 = dfws / 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 = dfws
			end
			if tower.Health.Value <= 0 then
				for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
					if v.Name == "Slash1" or v.Name == "Slash2" then
						v:Stop()
					end
				end
				creep.Humanoid.WalkSpeed = dfws
				local cs = script.Parent:Clone()
				cs.Parent = script.Parent.Parent
				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 = dfws
					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
						for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
							if v.Name == "Slash1" or v.Name == "Slash2" then
								v:Stop()
							end
						end
						creep.Humanoid.WalkSpeed = dfws
						local cs = script.Parent:Clone()
						cs.Parent = script.Parent.Parent
						script:Destroy()
					end
					wait()
				elseif script.ReachedPos.Value == 1 then
					creep.Humanoid.WalkSpeed = dfws
					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
						for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
							if v.Name == "Slash1" or v.Name == "Slash2" then
								v:Stop()
							end
						end
						creep.Humanoid.WalkSpeed = dfws
						local cs = script.Parent:Clone()
						cs.Parent = script.Parent.Parent
						script:Destroy()
					end
					wait()
				elseif script.ReachedPos.Value == 2 then
					creep.Humanoid.WalkSpeed = dfws
					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 = dfws
					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
						for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
							if v.Name == "Slash1" or v.Name == "Slash2" then
								v:Stop()
							end
						end
						creep.Humanoid.WalkSpeed = dfws
						local cs = script.Parent:Clone()
						cs.Parent = script.Parent.Parent
						script:Destroy()
					end
					wait()
				elseif script.ReachedPos.Value == 1 then
					creep.Humanoid.WalkSpeed = dfws
					local path = pathfindingService:CreatePath()
					path:ComputeAsync(creep.PrimaryPart.Position, game.Workspace.Top1.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.Top1.Position).Magnitude <= 10 then
						script.ReachedPos.Value = 2
						for i, v in pairs(creep.Humanoid.Animator:GetPlayingAnimationTracks()) do
							if v.Name == "Slash1" or v.Name == "Slash2" then
								v:Stop()
							end
						end
						creep.Humanoid.WalkSpeed = dfws
						local cs = script.Parent:Clone()
						cs.Parent = script.Parent.Parent
						script:Destroy()
					end
					wait()
				elseif script.ReachedPos.Value == 2 then
					creep.Humanoid.WalkSpeed = dfws
					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 FindNearestTorsoRadiant(position)
	local minDistance = math.huge
	local nearestTorso = nil

	for i, enemy in ipairs(targetCreepListRadiant) do
		local distance = (position - enemy:WaitForChild("HumanoidRootPart").Position).Magnitude
		if distance < minDistance then
			minDistance = distance
			nearestTorso = enemy:WaitForChild("HumanoidRootPart")
		end
	end

	return nearestTorso
end

local function FindNearestTorsoDire(position)
	local minDistance = math.huge
	local nearestTorso = nil

	for i, enemy in ipairs(targetCreepListDire) do
		local distance = (position - enemy:WaitForChild("HumanoidRootPart").Position).Magnitude
		if distance < minDistance then
			minDistance = distance
			nearestTorso = enemy:WaitForChild("HumanoidRootPart")
		end
	end

	return nearestTorso
end

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

	for _, tower in ipairs(game.Workspace:GetChildren()) do
		if tower.Name == "Tower1" or tower.Name == "Tower2" or tower.Name == "Tower3" or tower.Name == "Tower4" and tower.Faction.Value ~= faction and tower.Health.Value > 0 then
			local distance = (position - tower:WaitForChild("BasePart").Position).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 = FindNearestTorsoDire(script.Parent.HumanoidRootPart.Position)
				if nearestTorso and (script.Parent.HumanoidRootPart.Position - nearestTorso.Position).Magnitude < creepsattackrange 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 < creepsattackrange * 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 = FindNearestTorsoRadiant(script.Parent.HumanoidRootPart.Position)
				if nearestTorso and (script.Parent.HumanoidRootPart.Position - nearestTorso.Position).Magnitude < creepsattackrange 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 < creepsattackrange * 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 сделай так чтобы скрипт не выдавал ошибки в консоль