So while testing this Voting system where in the function, it picks 3 random maps and adds them into the table.
Whats the Issue?
The issue is that its only adding one map
What is happening?
function MapSys:OpenVoting()
table.clear(MapSys.Table) -- Removes Maps from table
local children = MapSys.Directory:GetChildren() -- Gets List of maps
for i = 1,MapSys.Max do task.wait()
local map = children[math.random(1, #children)] -- Random Map
if #MapSys.Table == MapSys.Max then break end -- maybe useless
if not table.find(MapSys.Table, map) then -- if map not found in table
table.remove(MapSys.Table table.find(MapSys.Table,map)) -- deletes map copy
table.insert(MapSys.Table, map) -- inserts map
print(map) -- prints map
end
end
print(MapSys.Table) -- prints table
end
Output:
03:02:03.920 Map5 - Server - VoteLib:22
03:02:03.937 Map1 - Server - VoteLib:22
03:02:03.953 Map4 - Server - VoteLib:22
03:02:03.954 ▼ {
[1] = Map4 -- Why only one map?
} - Server - VoteLib:26
I believe this is the problem: if #MapSys.Table == MapSys.Max then break end . This line is breaking the loop once the number of maps in the MapSys.Table table reaches MapSys.Max . Since MapSys.Max is set to 1 , the loop only runs once and only one map gets added to the table.
You can either increase the value of MapSys.Max to be higher than 1 or remove the break statement so that the loop can continue running and adding maps until the MapSys.Table table is full.
Also table.remove(MapSys.Table table.find(MapSys.Table,map)) is incorrect. The table.remove() function takes two arguments: the table and the index of the element to be removed. In this case, you’re trying to pass in table.find(MapSys.Table,map) as the index, but table.find() returns a boolean value indicating whether the element was found in the table, not the index of the element. To remove an element from a table by its value, you could try this:
local index = table.find(MapSys.Table, map)
if index then
table.remove(MapSys.Table, index)
end
MapSys.Directory = game.ServerStorage.Maps -- Location of Maps
MapSys.Table = {} -- table to store maps
MapSys.Max = 3 -- Max maps inside the table
function MapSys:OpenVoting()
table.clear(MapSys.Table)
local children = MapSys.Directory:GetChildren()
for i = 1,MapSys.Max do task.wait()
local map = children[math.random(1, #children)]
if not table.find(MapSys.Table, map) then
--table.remove(MapSys.Table, MapSys[map])
table.insert(MapSys.Table, map)
table.remove(children, table.find(children, map))
print(map)
end
end
print(MapSys.Table)
end
either way i fixed it, all i had to do was replace the Table and remove items from the GetChildren table
@Arylist that if statement was useless as the for loop only ran 3 times