How to make a npc attack multiple people at once

what im trying to do is make a npc be able to call a module multiple times for everyplayer thats in its max distance, right now im still stuck as the npc is just attacking the closest player

local rep = game.ReplicatedStorage
local humanoid = script.Parent.Humanoid
local maxdistance = 100
local mindistance = 50
local died = script.Parent.Dead

humanoid.Died:Connect(function()
	print("Died")
	died.Value = true
end)

while task.wait(0.5) do
	local playersInRange = {}

	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player and player.Character then
			local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
			if humanoidRootPart and player.Character:FindFirstChild("Humanoid") then
				local distance = (humanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).magnitude
				if distance < maxdistance then
					table.insert(playersInRange, {player = player, distance = distance})
				end
			end
		end
	end

	for _, playerInfo in pairs(playersInRange) do
		local player = playerInfo.player
		local distance = playerInfo.distance

		if distance <= mindistance and not died.Value then
			local humanoidrootpart = player.Character:FindFirstChild("HumanoidRootPart")
			if humanoidrootpart then
				local random = math.random(1, 5)
				local modelu = require(script.ModuleScript)

				if random == 1 then
					modelu.GasterBlaster(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 2 then
					modelu.BoneThrow(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 3 then
					modelu.BoneZone(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 4 then
					modelu.Quad(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 5 then
					modelu.RandomBlast(script.Parent.HumanoidRootPart, humanoidrootpart)
				end
			end
		end
	end

	if #playersInRange > 0 then
		local closestPlayer = playersInRange[1].player
		local closestDistance = playersInRange[1].distance

		for _, playerInfo in pairs(playersInRange) do
			if playerInfo.distance < closestDistance then
				closestPlayer = playerInfo.player
				closestDistance = playerInfo.distance
			end
		end

		if closestPlayer.Character then
			local closestHumanoidRootPart = closestPlayer.Character:FindFirstChild("HumanoidRootPart")
			if closestHumanoidRootPart then
				humanoid:MoveTo(closestHumanoidRootPart.Position)
			end
		end
	end
end
3 Likes

ive used roblox assistent but the ai couldnt understand what i mean

1 Like

im still stuck in this problem i hope any one can respond

1 Like

briefly looking at the code, it does these in the while loop:

get all players that is within `maxdistance` and add them into `playersInRange` array

for each of those players,
   **check that they are within `mindistance`** and the npc is not dead
   random attack

find the closest player among those players and move to them

so it should indeed “attack all players that is within min distance” (not just max distance)
but only “move to closest player”

modify to suit your desired logic

1 Like

so basicl your saying that the thing is swapped?

1 Like

basicly it only attacks one player while the other just trys to move to all player?

1 Like

it is currently attacking all players within min distance, and then move towards closest player

1 Like

well how do i make it so that for each player a module is fired, instead of just making it so that it attacks one player, moves to the next list then attacks another player, then so on

1 Like

im kinda confused on what your trying to say

1 Like

it is already “firing a module” for each player

1 Like

search the script and add the debug print to make sure

i was attacked at night multiple times by the npc @MastermindedSpider

no what im trying to say is that how do i make it so that it can use multiple attacks for multiple people, instead of just making it random attacks for random people

like i want it to do double attacks for each person

				local random = math.random(1, 5)
				lo  cal modelu = require(script.ModuleScript)

				if random == 1 then
					modelu.GasterBlaster(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 2 then
					modelu.BoneThrow(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 3 then
					modelu.BoneZone(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 4 then
					modelu.Quad(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 5 then
					modelu.RandomBlast(script.Parent.HumanoidRootPart, humanoidrootpart)
				end

so according to this part, it generates a random number, and then based on this checking, it do the corresponding attack.
you can first try remove all the checks, then it will do all those attacks to that single player

like so:

				local modelu = require(script.ModuleScript)
				modelu.GasterBlaster(script.Parent.HumanoidRootPart, humanoidrootpart)
				modelu.BoneThrow(script.Parent.HumanoidRootPart, humanoidrootpart)
				modelu.BoneZone(script.Parent.HumanoidRootPart, humanoidrootpart)
				modelu.Quad(script.Parent.HumanoidRootPart, humanoidrootpart)
				modelu.RandomBlast(script.Parent.HumanoidRootPart, humanoidrootpart)

without knowing what the actual module attack would behave,
to make any attack doubled, literally double each function calls

				local random = math.random(1, 5)
				local modelu = require(script.ModuleScript)

				if random == 1 then
					modelu.GasterBlaster(script.Parent.HumanoidRootPart, humanoidrootpart)
					modelu.GasterBlaster(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 2 then
					modelu.BoneThrow(script.Parent.HumanoidRootPart, humanoidrootpart)
					modelu.BoneThrow(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 3 then
					modelu.BoneZone(script.Parent.HumanoidRootPart, humanoidrootpart)
					modelu.BoneZone(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 4 then
					modelu.Quad(script.Parent.HumanoidRootPart, humanoidrootpart)
					modelu.Quad(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 5 then
					modelu.RandomBlast(script.Parent.HumanoidRootPart, humanoidrootpart)
					modelu.RandomBlast(script.Parent.HumanoidRootPart, humanoidrootpart)
				end

so you can now mix and match those different attacks. sort of like each random number generate a different combo:

				local random = math.random(1, 5)
				local modelu = require(script.ModuleScript)

				if random == 1 then -- combo 1, zone up a player, throw 3 bones to them
					modelu.BoneZone(script.Parent.HumanoidRootPart, humanoidrootpart)
					modelu.BoneThrow(script.Parent.HumanoidRootPart, humanoidrootpart)
					modelu.BoneThrow(script.Parent.HumanoidRootPart, humanoidrootpart)
					modelu.BoneThrow(script.Parent.HumanoidRootPart, humanoidrootpart)
				elseif random == 2 then
					-- ... other combos you like
				end

you sure that the script earlier will attack multiple people at once?, the earily earily script

i cant test it because im using a crappy computer to code

also the bonethrow module uses 60 bones, so i think it might crash the game

basicly i want no cooldowns, like how do i explain this

what im trying to achieve is to make it so that for each attack theres no cooldown, so i can do multiple attack at once with out having to go through the process of each attack one by one