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
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