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

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

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 comment

1

Put all the zombies in a list, then do zs[math.random(#zs)].

1 answer

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

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