I'm attempting to make a sequence happen after you kill all of the npcs in a game I am making, but I cannot get the script I made to work. When I kill them all it doesn't print "all zombies where killed". There are no errors. Can anyone help?
while true do wait (1) if workspace.Zobies:GetChildren() == nil then print("all zombies where killed") end end
use this: if #workspace.Zobies:GetChildren() == 0 then print("all zombies where killed") end
Right now, your code would loop every second and check if the array that GetChildren() returns does not exist, hence "nil". Since the array does in fact exist, the condition in the if statement will always be false.
The GetChildren() function "returns an array containing all of the instance's direct children." https://developer.roblox.com/en-us/api-reference/function/Instance/GetChildren
I'm going to assume that when a zombie is killed, it is also destroyed. To find the number of zombies left, we can use the length of table (#) operator.
while true do if #workspace.Zobies.GetChildren() == 0 then print("all zombies where killed") end task.wait() end
I'm also going to assume that you want to have some kind of a round-based system. For this, all I added was the round variable to the while loop and some wait logic to start the round again.
local function doRound() local round = true while round do if #workspace.Zobies:GetChildren() == 0 then print("all zombies where killed") round = false end task.wait() end print("Intermission | Round starting in 15 seconds") task.wait(15) -- zombie spawn logic here print("Round started") round = true end
Well, here you go, hope this helps.