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

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

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

✓ Accepted answer
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

2 comments

0

Thanks but, Now they don't spawn at all

0

Ok I got it working thank you, it was just the folder name was named differently.

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