Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make sure all my npc's have died?

So, this question arose by curiosity from another person's question I was trying to answer. I have a lot of enemy npcs in a folder, and i want a gate to open when all of them have died. Here is what I have so far, Any help is appreciated!

local part1 = game.Workspace.Part1
local part2 = game.Workspace.Part2
local t = 0
local npcf = game.Workspace.NPCF:GetChildren() -- npcf is the folder and I am getting all the npcs using GetChildren()

local TweenService = game:GetService("TweenService") -- these are the tweens which should play if it has died.

local part1 = game.Workspace.Part1

local goal = {}
goal.Position = Vector3.new(-6.197, 22.353, -67.553)

local tweenInfo = TweenInfo.new(5)

local tween1 = TweenService:Create(part1, tweenInfo, goal)

local TweenService = game:GetService("TweenService")

local part2 = game.Workspace.Part2

local goal = {}
goal.Position = Vector3.new(-23.852, 21.838, -66.892)

local tweenInfo = TweenInfo.new(5)

local tween2 = TweenService:Create(part2, tweenInfo, goal)

local npc2 = game.Workspace.NPC2


while true do
	for i = 1,#npcf do -- I have no clue what to do for multiple npcs and how to constantly check if ALL OF THEM HAVE DIED
		local npcnum = npcf[i]
		if npcnum.Humanoid.Health == 0 then
			print("npc"..i.."died")
		end
	end
	if npc2.Humanoid.Health == 0 then -- this one works, but its for only1 npc cuz it only checks constantly if 1 npc has died
		tween2:play()
	end
	wait()
end

1 answer

Instead of using

for i = x, y do

end

use

for i, v in pairs() do

end

as shown below:

local part1 = game.Workspace.Part1
local part2 = game.Workspace.Part2
local t = 0
local npcf = game.Workspace.NPCF:GetChildren()

local TweenService = game:GetService("TweenService")

local part1 = game.Workspace.Part1

local goal = {}
goal.Position = Vector3.new(-6.197, 22.353, -67.553)

local tweenInfo = TweenInfo.new(5)

local tween1 = TweenService:Create(part1, tweenInfo, goal)

local TweenService = game:GetService("TweenService")

local part2 = game.Workspace.Part2

local goal = {}
goal.Position = Vector3.new(-23.852, 21.838, -66.892)

local tweenInfo = TweenInfo.new(5)

local tween2 = TweenService:Create(part2, tweenInfo, goal)

local npc2 = game.Workspace.NPC2


while true do
    for i, npc in pairs(npcf) do -- Just loops through the table contents
        if npc.Humanoid.Health == 0 then
            print("npc"..i.."died")
        end
    end
    if npc2.Humanoid.Health == 0 then
        tween2:play()
    end
    wait()
end

8 comments

0

Hi I also tried to do for 1,v in pairs() do but how do I make sure all the different npcs have died using this?

0

i*

0

I'm not sure what you mean by different NPCs, could you explain further?

0

so I have a folder of npc and I want the gate to open when all of them have died, but how do I do that?

0

Sorry for the late response, was asleep. We're using GetChildren() to get all the npcs inside the folder, then looping through all of them to check if their health is 0.

0

I know that, but that still doesnt make the script realise whether ALL the npc's have died.

0

I dont want it to just open when 1 npc has died, i want it to open when all of them have died. how do I know that?

0

Oh, I thought NPCF was a folder, anyway, make a folder, put in all the NPCS there and use :GetChildren() on the folder.

This discussion is preserved as it was. New answers, comments, and votes are closed.