Spawn random enemy

How would i make a script that spawns a random enemy?

local bosses = bossfolder:WaitForChild("Bosses"):GetChildren()
local randomnum = math.random(1, #bosses)
-- spawn random boss
2 Likes

Assuming the bosses are models it should be pretty simple since you already got the random boss chosen as a number, Now we just need to find that model in the table and positioning it, then Parent it into the workspace.

local bosses = bossfolder:WaitForChild("Bosses"):GetChildren()
local randomnum = math.random(1, #bosses)

-- spawn random boss at predetermined position and orientation

local bossCloneChosen = bosses[randomnum]:Clone()
local spawnPositionAndOrientation = CFrame.new(0,0,0)
bossCloneChosen:SetPrimaryPartCFrame(spawnPositionAndOrientation)
bossCloneChosen.Parent = workspace
4 Likes

Kinda off topic, just a heads up: The new-ish Random datatype is usually better than math.random.

You can also do the boss choosing-thing as a one-liner like so:

local r = Random.new() --Only do this once 

...

local chosenBoss = bosses[r:NextInteger(#bosses)]:Clone()

Or split it into a reusable function:

local function chooseRandom(list)
    return list[r:NextInteger(#list)]
end

...

local chosenBoss = chooseRandom(bosses):Clone()
5 Likes