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

How would I make it re-roll if its taken?

Asked by
UPumpkin -34
5 years ago

I want something like.

local hi = math.random(1,2)

if hi == 1 then

re does the math.random(1,2) until hi == 2 

1 answer

Log in to vote
0
Answered by
zor_os 70
5 years ago
Edited 5 years ago

You'll want to use loops basically

local hi = math.random(1,2)

while hi ~= 2 do -- while hi is not equal to 2 loop
    wait()
    hi = math.random(1,2)
end

that will work if you only want 2 choices if you want more something like this will work

local taken = {} -- store all the used values
local hi = math.random(1,5) -- final unused value
local foundOne = false -- keep while loop going
while foundOne == false do
    wait()
    foundOne = true -- make true since it assumes it's not taken until proven wrong
    local hi = math.random(1,5) -- change hi to new value
    for i = 1,#taken do -- loop through all used values
        if taken[i] == hi then -- value is taken
            foundOne = false -- continue loop
        end
    end
end
table.insert(taken,hi) -- add value to used list
0
sorry if there's some errors i'm not in studio so I can't confirm everything works 100% but you get the gist of what needs to be done zor_os 70 — 5y
Ad

Answer this question