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

How do I make a variable pick a string that is not the same as another one?

Asked by 3 years ago

I want to know how to make a variable pick a string from another variable, but not be the same as other variables picking from the main variable.

I have a main variable where all the other ones pick strings from, it's name is Characters, now I have other 6 vars. where these pick randomly a string from Characters, I have it like this:

local UC1 = Characters[math.random(1, #Characters)]
local UC2 = Characters[math.random(1, #Characters)]
local UC3 = Characters[math.random(1, #Characters)]
local UC4 = Characters[math.random(1, #Characters)]
local UC5 = Characters[math.random(1, #Characters)]
local UC6 = Characters[math.random(1, #Characters)]

Afterwards it destroys the selected strings of each var. But sometimes it picks the same string as another var. so if anyone knows how to make each var. pick a string that is not the same as the others, I would appreciate it.

1 answer

Log in to vote
0
Answered by
kisty1 111
3 years ago
Edited 3 years ago

You could store the selected elements in an array and compare them with other randomly generated elements before inserting them into the selected elements array

local Selected = {}
for i = 1, 6 do
    local Item = Characters[math.random(1, #Characters)]
    while table.find(Selected, Item) do -- while Item is inside Selected
        Item = Characters[math.random(1, #Characters)]
    end
    table.insert(Selected, Item)
end
-- do stuff with selected items
Ad

Answer this question