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

Question about random variables and tables?

Here is an example script of me trying to make a simple cointoss:

local AllTeams = {"Red Team", "Blue Team", "Orange Team", "Purple Team", "White Team"}

local randomteam1 = AllTeams[math.random(#AllTeams)]
local randomteam2 = AllTeams[math.random(#AllTeams)]

--This is what i wanted to do but you cant do this--
local CoinToss = {randomteam1, randomteam2} 

local CoinTossWinner = CoinToss[math.random(#CoinToss)]

--How can I do this??

What this does is picks 2 random teams out of the "All Teams" table and what i wanted it to do after that, was I wanted it to pick a random variable so it could be like a coin toss, sadly i dont know how to do this

Any feedback is helpful, thanks.

2 comments

0

A random variable? Why do you need a random variable?

0

read the code.. I want the code to get 1 out of 2 variables randomly

1 answer

✓ Accepted answer
local teams = {"Team1", "Team2", "Team3", "Team4", "Team5"}

local randomTeam = teams[math.random(1, #teams)]
local randomTeam2 = teams[math.random(1, #teams)]

if randomTeam ~= randomTeam2 then -- make sure the number isn’t the same
    coinToss = {randomTeam, randomTeam2}
    local winner = coinToss[math.random(1, #coinToss)]
    print(winner) --will print your winner 
end

1 comment

0

hm maybe i did need to check lol

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