Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Question about random variables and tables?

Asked by 5 years ago
Edited 5 years ago

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.

0
A random variable? Why do you need a random variable? cringe_worthey 97 — 5y
0
read the code.. I want the code to get 1 out of 2 variables randomly MusicalDisplay 173 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago
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
0
hm maybe i did need to check lol MusicalDisplay 173 — 5y
Ad

Answer this question