Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get the value to change when the NPC dies so it respawns?

Asked by 3 years ago

This script spawns a random zombie from a folder on to a random block. But doen't respawn them.

I can't make it so that when of the created zombies dies it makes the number value go down, which in turn will spawn a replacement zombie. .

local NPCS = game.ReplicatedStorage.Enemies:GetChildren()
local number = 0
local max = 6

if number <= max then
    canspawn = true
    print("can spawn")
end 


while  canspawn do
    local ChosenNPC = NPCS[math.random(1, #NPCS)]
    local ChosenSpawn = math.random(1, 6)
    print(ChosenNPC)
    print(ChosenSpawn)
    wait(4)

    if ChosenNPC then
        clone = ChosenNPC:Clone()
        clone.HumanoidRootPart.Position = workspace.EnemySpawns:FindFirstChild("Spawn"..ChosenSpawn).Position
        clone.Parent = workspace.EnemySpawns.Spawned
        clone:MakeJoints()

        number = number + 1 
        print(number)
        if   clone.Humanoid.Health <= 0   then
            number = number -1
            print(number)
        end 

        if number >= max then
            canspawn = false
            print("Stoping spawning")
        end 


    end

end

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago
if   clone.Humanoid.Health <= 0   then
            number = number -1
            print(number)
        end

This wont work because it will run only once when the npc spawns meaning if the npc takes damage and dies, this wont run.

local NPCS = game.ReplicatedStorage.Enemies:GetChildren()
local max = 6

while true do
    if #workspace.Enemy.Spawned:GetChildren() ~= max then -- if the amount of enemies is not max, then it will continue to spawn
    local ChosenNPC = NPCS[math.random(1, #NPCS)]
    local ChosenSpawn = math.random(1, 6)
    print(ChosenNPC)
    print(ChosenSpawn)

    if ChosenNPC then
        clone = ChosenNPC:Clone()
        clone.HumanoidRootPart.Position = workspace.EnemySpawns:FindFirstChild("Spawn"..ChosenSpawn).Position
        clone.Parent = workspace.EnemySpawns.Spawned
        clone:MakeJoints()

        number = number + 1 
        print(number)
    end
    end
wait(4)

end

I edited some of the code so it works when the amount of enemies has changed. This doesn't get the amount to change but has the same result

0
Thanks but, Now they don't spawn at all Boss246813579 -6 — 3y
0
Ok I got it working thank you, it was just the folder name was named differently. Boss246813579 -6 — 3y
Ad

Answer this question