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

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

2 answers

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.

1 comment

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?

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