[already solved]

  1. What do you want to achieve? Keep it simple and clear!
    So the AI is actually like a zombie, The problem is the AI which only one AI moves and not the other one…?

  2. What is the issue? Include screenshots / videos if possible!
    The issue is only one AI works, And If I kill the moving AI sometimes the other one moves and sometimes does not. I don’t really get CollectionService, If so please help me with this.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes I have looked solutions about, CollectionService working only on one instance, None of them are working. There are some several edits on this script because I thought that would work on my script.
1 Like

This is only the AI script, No attacks yet

1 Like

I think that is because you put another for loop inside of it (the game.players:getplayers()). Try making a function that returns the tor of the player

1 Like

Also, do not use spawn(function(). Instead, use task.spawn or coroutine.wrap

2 Likes

This may be because of the return statement inside of your first two functions. If the loop immediately runs successfully, it will return the data for one single zombie. All of the other zombies are left without any data to go out of, since the for loops never run completely. That is also why when deleting a zombie, sometimes the other one starts to move. Inside of your spawn(function() end) , when you run getNearestTorso() and torsosInRange() data is on even sent as the first zombie.
A way to fix this is: inside of your spawn(function() end) , send the zombohumanoid and/or zomboroot in through your pathfinding functions. That way, you can loop successfully for all zombies

1 Like
—i may have missed some things
local collectionService = game:GetService("CollectionService")

chaseDist = 1000000000000

function getNearestTorso(zomboroot,torsos)
	local minDist = 1000000000 
	local minTor = nil
        for _, tor in torsos do
                if (zomboroot.Position - tor.Position).Magnitude < minDist then
			minTor = tor
		        minDist = (zomboroot.Position - tor.Position).Magnitude
		end
	end
	return minTor, minTor.Parent:FindFirstChild('Humanoid')
end

function torsosInRange(zomboroot)
	local foundTorsos = {}
	for i,plr in pairs(game.Players:GetPlayers()) do
		if plr.Character then
			local tor = plr.Character:FindFirstChild("HumanoidRootPart")
			if tor then
				local ignorelist = {zomboroot.Parent}
				
				for na,ni in pairs(plr.Character:GetDescendants()) do
					if ni.Parent:IsA("Accoutrement") or ni.Parent:IsA("Tool") then
						table.insert(ignorelist, ni)
					end
				end
				
				local searchray = Ray.new(zomboroot.Position, CFrame.new(zomboroot.Position, tor.Position).LookVector * chaseDist)
				local hit,pos = workspace:FindPartOnRayWithIgnoreList(searchray, ignorelist)
				
				if hit then
					if hit.Parent == plr.Character then
						local hu = plr.Character:FindFirstChild('Humanoid')
						if hu then
							if hu.Health > 0 then
								table.insert(foundTorsos, tor)
							end
						end
						
					end
				end
			end
		end	
        end
	return foundTorsos
end


spawn(function()

	for _,zomboroot in pairs(collectionService:GetTagged("ZombieRoot")) do

        local zombohum = zomboroot.Parent.Humanoid
	local damage = 0
	local damageCooldown = 1 -- in seconds
	local dmg_range = 3
	local lastDamageTime = time()

	while zombohum.Health > 0 do
		local foundTorsos = torsosInRange(zomboroot)

		if #foundTorsos > 0 then
			local dator, dahum = getNearestTorso(zomboroot,foundTorsos)

			while dator.Parent ~= nil and dahum.Parent ~= nil do

				foundTorsos = torsosInRange(zomboroot)

				if #foundTorsos > 0 then
					local newtor, newhum = getNearestTorso(zomboroot,foundTorsos)

					if newtor and newhum then -- SITUATION 1
						if newtor == dator then
							if dahum.Health > 0 then
								if (zomboroot.Position - dator.Position).Magnitude <= dmg_range and time() - lastDamageTime >= damageCooldown then
									-- damage the player
									lastDamageTime = time()
								end
								zombohum:MoveTo(dator.Position + CFrame.new(zomboroot.Position, dator.Position).LookVector * 6)
							else
								break
							end
						else -- SITUATION 3
							break
						end
					else
						
					end
				else -- SITUATION 2
					if (zomboroot.Position - dator.Position).Magnitude >= 25 or dahum.Health <= 0 then
						break
					end	
					zombohum:MoveTo(dator.Position)
					--print'chasing bend'	
				end
				wait(.1)
			end
		else
			-- IF NO ONE IS NEARBY --
			while #torsosInRange() == 0 do 
				local wanderAngle, wanderRay, hit, pos
				local failAngles = Instance.new("Model") 

				while zombohum.Health > 0 do
					repeat -- 2 loops is the key!
						wanderAngle = Instance.new('NumberValue', failAngles)
						wanderAngle.Name = math.random(-180,180)
						if failAngles:FindFirstChild(wanderAngle.Name) then
							wanderAngle:Destroy()
						end
					until not failAngles:FindFirstChild(wanderAngle.Name)


					wanderRay = Ray.new(zomboroot.Position, (zomboroot.CFrame * CFrame.Angles(0, math.rad(wanderAngle.Name), 0)).LookVector * chaseDist )
					hit,pos = workspace:FindPartOnRay(wanderRay, script.Parent)

					if pos then
						if (zomboroot.Position - pos).Magnitude >= 20 then
							break
						else
							wanderAngle:Clone().Parent = failAngles
						end
					end

					if #failAngles:GetChildren() >= 361 then
						break
					end
				end

				if (zomboroot.Position - pos).Magnitude >= 20 then
					--print'found reasonable path'
				elseif #failAngles:GetChildren() >= 361 then
					--print'no reasonable path'
				end

			zombohum:MoveTo(pos)
				local wtime = 0
				repeat
					wtime = wtime + wait(.1)
				until (zomboroot.Position - pos).Magnitude <= 8 or #torsosInRange() > 0 or wtime >= 4


			end 

		end

			wait(.1)
	end
	end
end)

I tried to put what you said by sending the zombohumanoid and zomboroot and it says attempt to index nil with ‘Parent’?, I don’t understand why, When it exist, Do I have to check if that zombie has the tag? I didn’t really took the whole script you send only a few lines that make it work. Still only one zombie works.

Anyways thanks for your help, I see what to do now.
Edit: I fixed it by my own

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.