0

# Can anyone help me with this script that is supposed to print something after all npcs are killed?

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


0

use this: if #workspace.Zobies:GetChildren() == 0 then print("all zombies where killed") end

0
HDMini 30
5 days ago

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

print("Intermission | Round starting in 15 seconds")

-- zombie spawn logic here

print("Round started")

round = true
end


Well, here you go, hope this helps.

0
So i tested the first one, but it never printed anything. I have the script placed in workspace, should it be placed somewhere else? Student20986 0 — 5d