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

How to use math.random in a way to get a name out of a folder?

So, I'm needing a question answered.. and like, I want to know how to use math.random to get object names in a certain folder for a minigame script I'm doing. Couldn't figure it out. So, If anyone is kind enough please help me out - this is not a request though.

1 comment

0

Post an attempt, what you have currently, or know you might need for this.

2 answers

✓ Accepted answer

This is actually pretty simple. All you would have to do is get the children of the folder and then set up math.random to select an entry of the table :GetChildren returns. For my example, my folder will have 5 entries.

local gameFolder = game:GetService("ReplicatedStorage").gameFolder
local games = gameFolder:GetChildren()

local function chooseGame()
	local num = math.random(1,5)
	local chosenGame = games[num]

	return chosenGame.Name
end

local nextGame = chooseGame()

print(nextGame)

5 comments

2

you can use math.random(1,#games) so you don't have to change the script in the future

0

Thanks for the script, never wanted one in the first place - just an explanation on how to make one. either way, thank you.

0

No problem :)

0

Also, thanks Dfzoz I didn't think of that

0

Txeer Your not allowed to assist with asking questions. ITS AGAINST THE COMMUNITY GUILDLINES. https://scriptinghelpers.org/help/community-guidelines

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