Hello! So, I’m currently scripting my project where very similary to moreorless.io, random 2 countries get picked and you need to choose which one is bigger.
I’ve already got a few folders:
And each folder contains information about that specific country:
My problem is, I don’t really know how to choose random 2 ones from that list. I really just need a path to the choosen folders.
I’ve looked on the devforums, but didn’t find anything exactly that could help me.
Thanks for any help and if anything is unclear, please let me know!
You should retrieve the Folder’s children via Database:GetChildren() and use Random.new() to get random numbers as the Random Object is way more better than math.random()
local RNG = Random.new() -- Random Number Generator
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Database = ReplicatedStorage:WaitForChild("Database")
local DatabaseChildren = Database:GetChildren()
-- Get 2 Different Databases
local RandomDatabase1 = DatabaseChildren[RNG:NextInteger(1, #DatabaseChildren)]
local RandomDatabase2 = DatabaseChildren[RNG:NextInteger(1, #DatabaseChildren)]
-- In-case one Database is the same as another one, We're gonna pick
-- Another Database until it's different from the another one.
while RandomDatabase1 == RandomDatabase2 do
RandomDatabase1 = DatabaseChildren[RNG:NextInteger(1, #DatabaseChildren)]
task.wait()
end
-- store all folders in a table
local folders = game.ReplicatedStorage.Database:GetChildren()
-- remove 1 folder from the table and save it as folder1 we remove it so there is 0 chance the same country gets selected twice
local folder1 = table.remove(folders, math.random(#folders))
-- this time there is no need to remove so we can save some performance by just selecting a random folder
local folder2 = folders[math.random(#folders)]
-- print the 2 selected countries
print(folder1.Name, folder2.Name)