Ad
Log in to vote
0

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

Asked by 6 days ago

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

Log in to vote
0
Answered by
LelSoy 0
6 days ago

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

Ad
Log in to vote
0
Answered by
HDMini 30
6 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
    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.

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 — 6d

Answer this question