Enemy AI stops working when multiple enemies are in-game

Hi! I’m working on an enemy AI system and I’ve run into an issue. If I have multiple enemies in the game, the code doesn’t work and no errors are returned. However if I have, let’s say 3, it’s working. It might be worth mentioning that I’m using StreamingEnabled. This is the code:

Client:

local CS = game:GetService("CollectionService")
local RS = game:GetService("ReplicatedStorage")

local DetectionAlertEvent = RS:WaitForChild("Enemy_Detection_Event")

local RUN = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local character = Player.Character
local HRP = character:WaitForChild("HumanoidRootPart")

local alerted = false
local distance = 50

local UpdateInterval = 1

local thugs = {}

local function check()
	for _,v in pairs(CS:GetTagged("Criminals")) do
		local is = table.find(thugs, v)
		if not is then
			table.insert(thugs, v)
		end
	end
end

RUN.RenderStepped:Connect(function()
	
	for _,v in pairs(thugs) do
		local EnemyToPlayer = (HRP.Position - v:WaitForChild("HumanoidRootPart", 1200).Position).Unit
		local distanceBetween = (HRP.Position - v:WaitForChild("HumanoidRootPart").Position).magnitude

		local EnemyFOV = v:WaitForChild("Head").CFrame.LookVector

		local dotProduct = EnemyToPlayer:Dot(EnemyFOV)
		
		if dotProduct >= 0.8 and distanceBetween <= 50 then
			if alerted == false then
				DetectionAlertEvent:FireServer(v)
			end
		end
	end

end)

coroutine.wrap(function()
	local LastUpdate = tick()
	check()
	game:GetService("RunService").Heartbeat:Connect(function()
		if tick() - LastUpdate >= UpdateInterval then
			LastUpdate = tick()
			check()
		end
	end)
end)()

Server:

local RS = game:GetService("ReplicatedStorage")

local EnemyModule = require(RS:WaitForChild("EnemyModule"))

local EnemyDetectionAlertEvent = Instance.new("RemoteEvent")
EnemyDetectionAlertEvent.Name = "Enemy_Detection_Event"
EnemyDetectionAlertEvent.Parent = RS

EnemyDetectionAlertEvent.OnServerEvent:Connect(function(Player, thug)
	EnemyModule:Follow(Player, thug)
end)

Module:

local attackRange = 4

local punchAnim = Instance.new("Animation")
punchAnim.AnimationId = "http://www.roblox.com/asset/?id=7412276132"

local cooldown = false

local damaged = false

local hittingLeft
local hittingRight

local module = {}

function module:Follow(Player, Thug)
	
	while wait() do
		local distance = (Thug:WaitForChild("HumanoidRootPart").Position - Player.Character:WaitForChild("HumanoidRootPart").Position).magnitude
		
		if distance < 50 then
			if distance > attackRange then
				Thug:WaitForChild("Humanoid"):MoveTo(Player.Character:WaitForChild("HumanoidRootPart").Position)
			end
			
			if distance <= attackRange and cooldown == false then
				cooldown = true
				
				local punchTrack = Thug:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(punchAnim)
				
				hittingLeft = Thug:WaitForChild("LeftHand").Touched:Connect(function(touch)
					if game.Players:GetPlayerFromCharacter(touch.Parent) then
						if damaged == false then
							touch.Parent:WaitForChild("Humanoid"):TakeDamage(5)
							damaged = true
						end
						
					end
				end)
				
				hittingRight = Thug:WaitForChild("RightHand").Touched:Connect(function(touch)
					if game.Players:GetPlayerFromCharacter(touch.Parent) then
						if damaged == false then
							touch.Parent:WaitForChild("Humanoid"):TakeDamage(5)
							damaged = true
						end
						
					end
				end)
				
				Thug:WaitForChild("HumanoidRootPart").Anchored = true
				
				punchTrack:Play()
				
				repeat wait() until punchTrack.IsPlaying == false
				
				Thug:WaitForChild("HumanoidRootPart").Anchored = false
				hittingRight:Disconnect()
				hittingRight = nil
				hittingLeft:Disconnect()
				hittingLeft = nil
				
				wait(3)
				
				cooldown = false
				damaged = false
			end
		end	
	end	
end

return module

2 Likes