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

Using a conditional statement with math.random?

For example, the code below.

function chooseRandom()
	local random = table[math.random(1, #table)]
	return random
end

How can I check if the selected random value is "Test", for example? If it is Test, return the value... if not, choose another random value until it meets that condition.

Check the comments for another example.

There's probably a simple way to do this that I'm missing. Does anyone know a solution to accomplish this?

3 comments

0

Then why randomize it if you only want one option to be picked?

0

I'm just using that as an example. Lets say there is a BoolValue associated with each value in the table. I need to check if the value is true. If it is, return the value... if not, choose another random value until it is true.

0

if random == true then return random else return nil????

1 answer

function chooseRandom()
	local Selected --store our value for selected value
	repeat Selected = table[math.random(1,#table)] until Selected == "Test"
	return Selected
end

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