(SOLVED) How to make randomized zombies spawn in a set amount at a certain point

I need help on a game I am making.

What the game is about.

So, a part of the game is to go through a Story-progressed zombie survival game, and I really am stuck on how to spawn a set amount of randomized zombies into the workspace to hunt the player(s) by reaching a certain point (e.g. touching a part to spawn them.).

I need help on how to do this without using folders to limit spawns. (Certain weapons will not hurt the zombies if they are in a folder in the workspace.)

Any help is appreciated :slight_smile:

Game link (It is a work in progress, no zombies spawn in the zombie game.): Game

2 Likes

Well, If you have the zombie model the only thing that you have to do is the following:

local amount = 10
local spawnPart = workspace.Spawn
local zombieTemplate = game.ReplicatedStorage.Zombie
local timeToWait = 2

for i = 1,10,1 do
    local Zombie = zombieTemplate:Clone()
    Zombie.Parent = workspace
    Zombie:SetPrimaryPartCFrame(spawnPart.CFrame * CFrame.new(Vector3.new(0,4,0))
    wait(timeToWait)
end

If you want to learn how to create a zombie I would check out this:

https://create.roblox.com/docs/building-and-visuals/animation/using-animations

https://create.roblox.com/docs/mechanics/pathfinding

If you are a beginner to scripting try these:

https://create.roblox.com/docs/tutorials/scripting/basic-scripting/intro-to-scripting#looping-code

1 Like

I do have the zombie models made, and thank you for that help. :slight_smile:

Also, if possible, is there a way to make it so that the script runs when you touch a part?

Oh, yes of course!

local amount = 10
local spawnPart = workspace.Spawn
local zombieTemplate = game.ReplicatedStorage.Zombie
local timeToWait = 2
local partToBeTouched = workspace.TouchPart

partToBeTouched.Touched:Connect(function(playerWhoTouched)
    for i = 1,10,1 do
    local Zombie = zombieTemplate:Clone()
    Zombie.Parent = workspace
    Zombie:SetPrimaryPartCFrame(spawnPart.CFrame * CFrame.new(Vector3.new(0,4,0))
    wait(timeToWait)
end
end)

1 Like

Thank you very much, one final question.

Is it possible to randomise the spawns? if you have an answer I would love to hear it.
I cannot thank you enough for helping me. :smile:

local amount = 10
local spawnParts = {workspace.Spawn0, workspace.Spawn1, workspace.Spawn3}
local zombieTemplate = game.ReplicatedStorage.Zombie
local timeToWait = 2
local partToBeTouched = workspace.TouchPart

partToBeTouched.Touched:Connect(function(playerWhoTouched)
    for i = 1,10,1 do
    local Zombie = zombieTemplate:Clone()
    Zombie.Parent = workspace
    Zombie:SetPrimaryPartCFrame(spawnParts[math.random(1, #spawnParts)].CFrame * CFrame.new(Vector3.new(0,4,0))
    wait(timeToWait)
end
end)

This should work!

Thanks for that, but I think I may have worded my question confusingly.

I meant to ask if there was a way to randomise the zombies that spawn.

So sorry about that.

Do you mean like the amount of the zombies?

I mean types of zombies.
e.g. Normal Zombies, Fire zombies, snow zombies, etc.

local amount = 10
local spawnPart = workspace.Spawn
local zombieTypes = {game.ReplicatedStorage.Normal, game.ReplicatedStorage.Fire, game.ReplicatedStorage.Water}
local timeToWait = 2
local partToBeTouched = workspace.TouchPart

partToBeTouched.Touched:Connect(function(playerWhoTouched)
    for i = 1,10,1 do
    local Zombie = zombieTypes[math.random(1, #zombieTypes)]:Clone()
    Zombie.Parent = workspace
    Zombie:SetPrimaryPartCFrame(spawnPart.CFrame * CFrame.new(Vector3.new(0,4,0))
    wait(timeToWait)
end
end)

This should work for you.

1 Like

This is very helpful, I am so grateful for this.

It spawned like 30 zombies when I tried it.

@Nycrov I think the script isn’t working right, It spawns more than 10 zombies when I try it in game.

In roblox, touching a part is not an event that will fire when you touch it for the first time!
It will fire when you move while in contact with the part. So if you walk on the part, it will fire multiple times. You would have to add a cooldown.

Thanks for the advice, you are a good scripter :smiley:

You are soon gonna be one too (: