local W=script.Parent local S=require(game.ReplicatedStorage.Modules.EntityData)local v={}function GetTableLength(W)local S=0 for W,v in next,W do S=S+1 end return S end addAnt=coroutine.wrap(function()while wait(5)do if GetTableLength(v)<3 then local S=game.ServerStorage.Creatures["\083\099\097\118\101\110\103\101\114 \065\110\116"]:Clone()S:SetPrimaryPartCFrame(W.PrimaryPart.CFrame*CFrame.new(0,5,0))S.Parent=W v[S]={carrying=nil;target=nil;destination=S.PrimaryPart.Position;lastOrder=tick();lastScan=tick();anims={}}for W,G in next,game.ReplicatedStorage.NPCAnimations.Ant:GetChildren()do v[S].anims[G.Name]=(S:WaitForChild("\072\117\109")):LoadAnimation(G)end;(S:WaitForChild("\072\101\097\108\116\104")).Changed:connect(function()S.PrimaryPart.Hurt.Pitch=S.PrimaryPart.Hurt.OriginalPitch.Value+math.random(-100,100)/100 S.PrimaryPart.Hurt:Play()end)S.AncestryChanged:connect(function()v[S]=nil end)end wait(55)end end)addAnt()function Seeking(W,S)for W,v in pairs(W)do if v==S then return true end end return false end manageAnts=coroutine.wrap(function()while wait(1)do local G={}for A,R in next,v do spawn(function()if A and(A.PrimaryPart and(A:FindFirstChild("\072\117\109")and(A.Hum.Health>0 and A:IsDescendantOf(workspace))))then v[A].lastScan=tick()if not v[A].carrying then local W,R=nil,math.huge for v,f in next,workspace.Critters:GetChildren()do if not Seeking(G,f)and(f:FindFirstChild("\072\105\116\083\104\101\108\108")and S.All[f.Name].abductable)then local S,v=A.PrimaryPart,f.PrimaryPart if not v then v=f:FindFirstChildOfClass("\066\097\115\101\080\097\114\116")end local G=(v.Position-S.Position).magnitude if G<R then W=f R=G end end end if A and W then v[A].destination=W.PrimaryPart.Position v[A].target=W table.insert(G,W)v[A].target.AncestryChanged:connect(function()v[A].target=nil end)end else v[A].destination=W.PrimaryPart.Position+Vector3.new(0,3,0)end if v[A].destination then A.Hum:MoveTo(v[A].destination)if not v[A].anims.AntWalk.IsPlaying then v[A].anims.AntWalk:Play()end if R.target and not R.carrying then local W=(A.PrimaryPart.Position-v[A].target.PrimaryPart.Position).magnitude if W<5 then local W=game.ServerStorage.Misc["\065\098\100\117\099\116\101\100 \083\104\101\108\108\121"]:Clone()R.carrying=W W.Shell.Material=R.target.Shell.Material W.Shell.Color=R.target.Shell.Color W.ActualName.Value=R.target.Name R.target:Destroy()W.Parent=A v[A].anims.AntWalk:Stop()v[A].anims.AntHold:Play()A.PrimaryPart.Chatter:Play()A.PrimaryPart.ShellyAbduct:Play()local S=Instance.new("\077\097\110\117\097\108\087\101\108\100")S.Parent=A.PrimaryPart S.Part0=A.PrimaryPart S.Part1=W.PrimaryPart S.C0=CFrame.new(-0.4,.4,-1.6)*CFrame.Angles(0,math.rad(90),0)end elseif R.carrying then local W=(A.PrimaryPart.Position-v[A].destination).magnitude if W<7 then A.Hum:MoveTo(A.PrimaryPart.Position)A.PrimaryPart.Chatter:Play()v[A].anims.AntHold:Stop()v[A].anims.AntWalk:Stop()v[A].carrying:Destroy()v[A].carrying=nil v[A].destination=nil v[A].target=nil end end end end end)end end end)manageAnts()
--Fixed some stuff and made ants go after diffrent targets.
local hill = script.Parent
local EntityData = require(game.ReplicatedStorage.Modules.EntityData)
local activeAnts = {}

function GetTableLength(tab)
	local total = 0
	for _, v in next, tab do
		total = total + 1
	end
	return total
end

addAnt = coroutine.wrap(function()
	while wait(5) do
		if GetTableLength(activeAnts) < 3 then
			local newAnt = game.ServerStorage.Creatures["Scavenger Ant"]:Clone()
			newAnt:SetPrimaryPartCFrame(hill.PrimaryPart.CFrame * CFrame.new(0, 5, 0))
			newAnt.Parent = hill

			activeAnts[newAnt] = {
				["carrying"] = nil,
				["target"] = nil,
				["destination"] = newAnt.PrimaryPart.Position,
				["lastOrder"] = tick(),
				["lastScan"] = tick(),
				["anims"] = {},
			}

			for _, anim in next, game.ReplicatedStorage.NPCAnimations.Ant:GetChildren() do
				activeAnts[newAnt].anims[anim.Name] = newAnt:WaitForChild("Hum"):LoadAnimation(anim)
			end

			newAnt:WaitForChild("Health").Changed:connect(function()
				newAnt.PrimaryPart.Hurt.Pitch = newAnt.PrimaryPart.Hurt.OriginalPitch.Value
					+ (math.random(-100, 100) / 100)
				newAnt.PrimaryPart.Hurt:Play()
			end)

			newAnt.AncestryChanged:connect(function()
				activeAnts[newAnt] = nil
			end)
		end
		wait(55)
	end
end)

addAnt()

function Seeking(array, target) --Queen ant keeps track of her workers. ;)
	for i, v in pairs(array) do
		if v == target then
			return true
		end
	end
	return false
end

manageAnts = coroutine.wrap(function()
	while wait(1) do
		local seeking = {}
		for ant, antData in next, activeAnts do
			spawn(function()
				if
					ant
					and ant.PrimaryPart
					and ant:FindFirstChild("Hum")
					and ant.Hum.Health > 0
					and ant:IsDescendantOf(workspace)
				then
					activeAnts[ant].lastScan = tick()
					-- scan for nearest Shelly
					if not activeAnts[ant].carrying then
						local nearestShelly, closestDist = nil, math.huge

						for _, creature in next, workspace.Critters:GetChildren() do
							if
								not Seeking(seeking, creature)
								and creature:FindFirstChild("HitShell")
								and EntityData.All[creature.Name].abductable
							then
								local a, c = ant.PrimaryPart, creature.PrimaryPart

								if not c then
									c = creature:FindFirstChildOfClass("BasePart")
								end

								local dist = (c.Position - a.Position).magnitude

								if dist < closestDist then
									nearestShelly = creature
									closestDist = dist
								end
							end
						end

						if ant and nearestShelly then
							activeAnts[ant].destination = nearestShelly.PrimaryPart.Position
							activeAnts[ant].target = nearestShelly
							table.insert(seeking, nearestShelly)

							activeAnts[ant].target.AncestryChanged:connect(function()
								activeAnts[ant].target = nil
							end)
						end
					else
						activeAnts[ant].destination = hill.PrimaryPart.Position + Vector3.new(0, 3, 0)
					end

					if activeAnts[ant].destination then
						ant.Hum:MoveTo(activeAnts[ant].destination)
						if not activeAnts[ant].anims.AntWalk.IsPlaying then
							activeAnts[ant].anims.AntWalk:Play()
						end

						if antData.target and not antData.carrying then
							local dist = (ant.PrimaryPart.Position - activeAnts[ant].target.PrimaryPart.Position).magnitude
							if dist < 5 then
								-- let's get a new shelly
								local abductedShelly = game.ServerStorage.Misc["Abducted Shelly"]:Clone()
								antData.carrying = abductedShelly
								abductedShelly.Shell.Material = antData.target.Shell.Material
								abductedShelly.Shell.Color = antData.target.Shell.Color
								abductedShelly.ActualName.Value = antData.target.Name
								--game.ReplicatedStorage.Relay.NPCAttack:Fire(antData.target,math.huge)
								antData.target:Destroy()
								abductedShelly.Parent = ant
								activeAnts[ant].anims.AntWalk:Stop()
								activeAnts[ant].anims.AntHold:Play()
								ant.PrimaryPart.Chatter:Play()
								ant.PrimaryPart.ShellyAbduct:Play()
								-- weld the shelly to the torso
								local weld = Instance.new("ManualWeld")
								weld.Parent = ant.PrimaryPart
								weld.Part0 = ant.PrimaryPart
								weld.Part1 = abductedShelly.PrimaryPart
								weld.C0 = CFrame.new(-0.4, 0.4, -1.6) * CFrame.Angles(0, math.rad(90), 0)
							end
						elseif antData.carrying then
							local dist = (ant.PrimaryPart.Position - activeAnts[ant].destination).magnitude
							if dist < 7 then
								ant.Hum:MoveTo(ant.PrimaryPart.Position)
								ant.PrimaryPart.Chatter:Play()
								activeAnts[ant].anims.AntHold:Stop()
								activeAnts[ant].anims.AntWalk:Stop()
								activeAnts[ant].carrying:Destroy()
								activeAnts[ant].carrying = nil
								activeAnts[ant].destination = nil
								activeAnts[ant].target = nil
							end
						end
					end
				end
			end) --Help prevent breaking and faster responding times for ants.
		end --Shouldn't error now though.
	end
end)

manageAnts()
