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

How can i use math.random() to spawn different types of zombies in my zombie game?

Asked by 5 years ago
Edited 5 years ago

For my zombie game i need to make different types of zombies so the game does not get too easy. Also if its possible, config how rare the zombies are. Here is the script : ~~~~~~~~~~~~~~~~~

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

~~~~~~~~~~~~~~~~~ What i want is different types of zombies spawning using math.random() also when replying how to do it i need the position.

EDIT: If there are any other ways i can do the other types of zombies and its shorter, ill accept.

1
Put all the zombies in a list, then do zs[math.random(#zs)]. fredfishy 833 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I might have an idea:

math.randomseed(tick()) -- Adds a bit more randomness
local CS = game:getService("CollectionService")
local zombieTypes = [game.ReplicatedStorage.Zombie,game.ReplicatedStorage.Zombie2] -- list of zombies
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 = zombieTypes[math.random(1,#zombieTypes)]: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

Ad

Answer this question