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

need help to stop script from spawning in too many zombies?

Asked by 3 years ago

so i got this script that controls the main spawning for zombies

local CS = game:getService("CollectionService")

while true do
    wait(5)
    for i, v in pairs(CS:GetTagged("spawner")) do
        if game.ReplicatedStorage.Values.gameInProgress.Value == true then
            if game.ReplicatedStorage.Values.zombiesRemaining.Value > 0 then
                local NPC = game.ReplicatedStorage.Zombie:Clone()
                NPC.Parent = v
                NPC.HumanoidRootPart.CFrame = v.CFrame
                CS:AddTag(NPC, "Zombie")
                game.ReplicatedStorage.Values.zombiesRemaining.Value = game.ReplicatedStorage.Values.zombiesRemaining.Value - 1
            end
        end
    end
end

but i want to make a cap of like 24 - 30 zombies allowed at a time. when the rounds get higher too many zombies spawn into the map. but i dont know how to write it out. an idea i had was this

local CS = game:getService("CollectionService")

while true do
    wait(5)
    for i, v in pairs(CS:GetTagged("spawner")) do
        if game.ReplicatedStorage.Values.gameInProgress.Value == true then
            if game.ReplicatedStorage.Values.zombiesRemaining.Value > 0 then
                local NPC = game.ReplicatedStorage.Zombie:Clone()
                NPC.Parent = v
                NPC.HumanoidRootPart.CFrame = v.CFrame
                CS:AddTag(NPC, "Zombie")
                game.ReplicatedStorage.Values.zombiesRemaining.Value = game.ReplicatedStorage.Values.zombiesRemaining.Value - 1
            end
            if game.ReplicatedStorage.Values.gameInProgress.Value == true then
                if game.ReplicatedStorage.Values.zombiesRemaining.Value < 24 then

                end

            end
        end
    end
end

but i honestly dont know how to write out "stop spawning" .

0
Did you make this code? WideSteal321 773 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

So, I found a few flaws in your code.

In first like, make 'g' capital.

local CS = game:GetService("CollectionService")

Since, your zombie spawning script (script 2), [on Line 6 and Line 14], you are checking the same value from Replicated Storage again and again. So, I prefer you to remove the 'if' statement of Line 14.

if game.ReplicatedStorage.Values.gameInProgress.Value == true then

Also, you said you want to spawn 24-30 zombies. So, at start you can create a local variable with math.random.

local ZombieSpawn = math.random(24, 30) -- 24 is minimum and 30 is maximum

At line 15, instead of:

if game.ReplicatedStorage.Values.zombiesRemaining.Value < 24 then

-- You can do:

if game.ReplicatedStorage.Values.zombiesRemaining.Value < ZombieSpawn then 

If you want to stop the spawning of zombies, you can simply add break after the line:

if game.ReplicatedStorage.Values.zombiesRemaining.Value < ZombieSpawn then 
    break
end

Make sure that 'break' will exit the loop. So, I recommend you to add a function or trigger(if/else statements or Remote Events) to trigger the zombie loop again.

Lemme know if it helped!

0
ok i think you're on the right track.. but its still spawning zombies above that count. im setting the math values to (2, 3) since round 1 has a total of 6 zombies nick2222 20 — 3y
0
Try checking the values of zombiesRemaining in Replicated Storage manually. BestCreativeBoy 1395 — 3y
Ad

Answer this question