-
What do you want to achieve?
I want to generate 3 random numbers, store them in an array, and detect / replace any duplicates within it. -
What is the issue?
If the values all come out different, the game works fine, but if a duplicate is found then it breaks with this error in the dev console. -
What solutions have you tried so far?
I’ve tried alternative methods of generating a new number then setting the value to that new number,
I’ve tried looking around the Developer Forum for ways to get values from indexs,
And I’ve looked around the Developer Forum for ways to do what I’m asking, the best I got was making a seperate table / array and adding it to there if it wasn’t already in the new table / array, it worked but it means if I had a duplicate number it wouldn’t add it and I wouldn’t have 3 values, which is why I’ve attempted to replace the value.
If possible I’d like to repeat the process of making a replacement random number until it is not found, instead of just replacing it with another random number as that could have a (small, but possible) chance of being another duplicate.
Any help or ideas would be greatly appreciated.
local AmtOfMaps = 0
for i, v in pairs(SERVER_MAPS) do
AmtOfMaps = AmtOfMaps + 1
end
print(AmtOfMaps.." maps in storage.")
local randomValues = {math.random(1, AmtOfMaps), math.random(1, AmtOfMaps), math.random(1, AmtOfMaps)}
print("randVal: "..randomValues[1])
print("randVal: "..randomValues[2])
print("randVal: "..randomValues[3])
local newValueTable = {}
for _, value in ipairs(randomValues) do
if not table.find(newValueTable,value) then -- broke??? check when u have time
table.insert(newValueTable,value)
else
table.insert(newValueTable, math(1, AmtOfMaps))
end
end
print("newVal: "..newValueTable[1])
print("newVal: "..newValueTable[2])
print("newVal: "..newValueTable[3])