Basically there are 3 rooms, it randomly picks one, after it picked one, it should remove it from the table and then pick again and again until no rooms are left.
What am I doing wrong?
local LevelOne = workspace.GameLevels.LevelOne
local TableOne = {LevelOne.Room1, LevelOne.Room2, LevelOne.Room3}
function start(player)
repeat
for i = 1, #TableOne do
local Room = TableOne[math.random(#TableOne)]
if TableOne[i] == Room then
table.remove(TableOne, i)
end
end
until TableOne == {}
print("Done")
end
TableOne will never equal {} because {} is an entirely new table that is created with a different reference and so this reference will not equal to the initial TableOne
*Something like printing a table will get you the table reference
Example print:
There are a few issues with your code, and I’ll explain why step by step:
Issue 1:
for i = 1, #TableOne do
local Room = TableOne[math.random(#TableOne)]
You’re giving i the value “i” the size of the table “TableOne” and that’s fine, this means the “i” value will take values from 1 to 3 in this case. You’re also assigning “Room” with a random value in “TableOne”, which is perfectly fine, but you have to keep this in mind.
if TableOne[i] == Room then
table.remove(TableOne, i)
end
Now here’s the main issue, TableOne[i] is the value that’s currently iterating so this means it may never be true since Room could be any other value, therefore the table could never get empty.
Issue 2:
until TableOne == {}
This will always return false, since you’re actually asking the VM whether these 2 tables have the same memory address, the latter is a completely separate table which has no associations with the former one.
Solution:
local LevelOne = workspace.GameLevels.LevelOne
local TableOne = {LevelOne.Room1, LevelOne.Room2, LevelOne.Room3}
function start(player)
repeat
local number = math.random(1,#TableOne)
local Room = TableOne[number]
table.remove(TableOne, number)
--Do your stuff here after it gets a room
until not next(TableOne) --This will make sure the table is 100% empty.
print("Done")
end