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

(Easy?) How would i pick a random variables ?

So, i want a script to pick between Map1 or Map2, so it will change the value. I don't quite know how this would work though, i have the variables here,

local Map1 = script.Parent.Map1
local Map2 = script.Parent.Map2

^^ those are the values, but i need this script to change / set one of those values randomly, example: Map1.Value = true

Note: I just need the value set randomly, nothing else.

Simplified question: How could i make a script randomly change the value on one of those variables.

Maybe using math.Random or math.randomseed()?

Thanks, killerbrasko2002

1 answer

✓ Accepted answer
local rng = Random.new()

local Map1 = script.Parent.Map1
local Map2 = script.Parent.Map2

local currentMap = rng:NextNumber() < 0.5 and Map1 or Map2

currentMap will now be a randomly chosen map from the two options.

Edit:

If you wanted to expand it to have more than two maps, you could do this:

local rng = Random.new()

local maps = {}
maps[#maps+1] = script.Parent.Map1
maps[#maps+1] = script.Parent.Map2
maps[#maps+1] = script.Parent.Map3
-- ..etc

local currentMap = maps[rng:NextInteger(1, #maps)]

Second Edit:

To change one of Map1 or Map2 to true you can do this:

local rng = Random.new()

local Map1 = script.Parent.Map1
local Map2 = script.Parent.Map2

-- In this case, either map will have a 50% chance of being set to true. You can also set the opposite value to false below if needed.
if rng:NextNumber() < 0.5 then
	Map1.Value = true
else
	Map2.Value = true
end

3 comments

0

but i need to set the map1 to true or false, not a number

0

I see now - See my second edit above

0

Thanks

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