How can I randomly pick map from folder then clone it?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am ‘Attempting’ to create a tower of hell style game but 2D.

  1. What is the issue? Include screenshots / videos if possible!

The issue is I’ve looked all over, and I can’t seem to randomly pick it from my folder. (I made sure all my paths are correct, so that’s not the problem.)

local RGM = game.ServerStorage.Stages.s1st

local randomMap = RGM[math.random(#1,RGM:GetChildren())]
randomMap:Clone().Parent = workspace.CurrentStages.c1st

image
The error down below seems to lead to here when I click it.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried to look in scripting helpers, the devforum, and the developer hub but can’t seem to find any.

Thanks!

Should be

local randomMap = RGM:GetChildren()[math.random(1,#RGM:GetChildren())]

You were trying to get the length of 1

2 Likes

Thank you for your great help, EmbatTheHybrid, it works!

I’m going to split that up because it’s doing a lot of stuff at once, to help you easier understand.

First you want to reference your folder:

local ServerStorage = game:GetService("ServerStorage")
local mapFolder = ServerStorage.Stages.s1st

Once you have the folder, you want to get a table of all the maps inside the folder. Then, we can pick randomly since they will all be in the table. To get a table, we use GetChildren(), as that returns a table of the children.

local maps = mapFolder:GetChildren()

So we have the table. Now we need to get a random number we will use to index the table. We want the random number to be between 1 and the number of maps inside of the folder. So we will do:

local randomIndex = math.random(1, #maps) 

This will give you the random number. Now with the number, you can index and find a map.

local randomMap = maps[randomIndex]

You now have a reference to the randomMap, and you can do whatever with it. In your case:

local randomClone = randomMap:Clone()
randomClone.Parent = workspace.CurrentStages.c1st

And putting it all together:

local ServerStorage = game:GetService("ServerStorage")
local mapFolder = ServerStorage.Stages.s1st

local maps = mapFolder:GetChildren()

local randomIndex = math.random(1, #maps) 

local randomMap = maps[randomIndex]

local randomClone = randomMap:Clone()
randomClone.Parent = workspace.CurrentStages.c1st
1 Like

you can do it in just 1 line of code and it’ll work:

 game:GetService("ServerStorage").Stages.s1st:GetChildren()[1, #game:GetService("ServerStorage").s1st:GetChildren()]:Clone().Parent = workspace.CurrentStages.c1st
1 Like

Lol, this is neat! It’s definitely bad programming practice though. It’s important to take readability in account and should probably not be sacrificed for brevity, unless the brevity is also easily understandable.

1 Like

Hmm… well you’re right, I just did it to show you can do it in a minimal amount of lines like @OP’s original code would’ve done.

1 Like