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

How do I make a Spawner that selects one random humanoid out of 5 or more different types?

Asked by 4 years ago

Basically I'm making a zombie survival themed game, but I'm not quite sure how to make it so the spawner for the enemy's/zombies are randomised. Like you have fast zombies and zombie bosses, also it should be based on percentage, so one type is more rare to spawn then another. If anyone can help me out or guide me through what the best way is to script something like this.

1 answer

Log in to vote
1
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You could use math.random.

Before we begin, let me give you an example of math. random:

local number = math.random(1,3)
print(number)

Output: 1, 2, or 3.

Okay, now to the coding:

First, let's get the number of players.

local Players = game.Players:GetPlayers() -- gettings all players
local nplrs = #Players -- number of players
print(nplrs)

Output: The number of players.

Okay, now let's use the code the get a player's name

local Players = game.Players:GetPlayers() -- gettings all players
local nplrs = #Players -- number of players
local Randomplayer -- random player
     Randomplayer = Players[math.random(1, nplrs)] -- random player
print(RandomPlayer.Name)

Output: A random player's name.

Now, let's get a random humanoid:

local Players = game.Players:GetPlayers() -- gettings all players
local nplrs = #Players -- number of players
local Randomplayer  -- random player
if nplrs >= 5 then -- if more or = to 5 then
    Randomplayer = Players[math.random(1, nplrs)] -- random player
    local randomplayerhumanoid = Randomplayer.Character.Humanoid -- the random humanoid
end

More information: https://developer.roblox.com/en-us/api-reference/lua-docs/math

https://developer.roblox.com/en-us/api-reference/class/Player

Ad

Answer this question