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

How to add a random score using math.random()?

Asked by 5 years ago

Explained better: I am having fun with math.random() and I wanted it to forcefully add either 7, 6 or 3 points to the score variable.. Here is the script:

local Team1Score = 0
local Team2Score = 0
local conditionalvar= true

--I dont know how to do this so i just thought of the only way that might have worked..
local addon1 = Team1Score + 7
local addon2 = Team2Score + 7
local addon3 = Team1Score + 6
local addon4 = Team2Score + 6
local addon5 = Team1Score + 3
local addon6 = Team2Score + 3

-- I think it will only work in a conditional so i did this
if conditionalvar == true then
    local aryAddon = {addon1, addon2, addon3, addon4, addon5, addon6}
    randomAddon = aryAddon[math.random(1,#aryAddon)]
    return randomAddon -- now I just did this not knowing if it would work or not...
end

--now i dont know how to make it do the variable....


Sorry if it isn't explained well enough! if you have questions just comment below and ill answer.. I'm on a time crunch, but I would really like to know! Thanks for feedback!

1 answer

Log in to vote
1
Answered by
mattscy 3725 Moderation Voter Community Moderator
5 years ago

Something like this might work out better:

local Options = {3,6,7}

local Team1Score = 0
local Team2Score = 0

local function AddToScore(Team)
    local Amount = Options[math.random(1,#Options)]
    if Team == 1 then
        Team1Score = Team1Score + Amount
    else
        Team2Score = Team2Score + Amount
    end
end

print(Team1Score)
AddToScore(1)
print(Team1Score)

Where you just have a table of outcomes, and you use math.random to choose a random outcome to add to the score.

0
You should be using Random.new(), its far better then math.random and unbiased. 3dsboy08 1 — 5y
Ad

Answer this question