Hello, I am looking to Improve this map vote setup system I made, this is the first one I made so I’m not sure if there is a better way to do this
What this does currently is loop forever until it can get 3 Maps according to the Limit
I will Walk through the Module:
local MapService = {} -- Module Table
MapService.Maps = game.ServerStorage.Maps
MapService.MapsToVote = {} -- Table that stores Maps
MapService.MaxMaps = 3 -- Limit on how many maps you can vote
function MapService:SetVoting()
table.clear(MapService.MapsToVote) -- Clears table
for i = 1,math.huge do task.wait() -- infinite loop until gets all maps
local children = MapService.Maps:GetChildren() -- gets children of folder
local map = children[math.random(1, #children)] -- picks a random maps
if #MapService.MapsToVote == MapService.MaxMaps then break end
-- if the Number of items in the map is equal to the Limit, If so, break
if not table.find(MapService.MapsToVote, map) then -- If map not inside table
table.insert(MapService.MapsToVote, map) -- inserts map into table
print(map.Name) -- prints map name
end
end
print(MapService.MapsToVote) -- Prints table
end
function MapService:GetVotes()
-- unfinished :(
end
return MapService -- returns table
local MapService = {} -- Module Table
MapService.Maps = game:GetService("ServerStorage").Maps
MapService.MapsToVote = {} -- Table that stores Maps
MapService.MaxMaps = 3 -- Limit on how many maps you can vote
function MapService:SetVoting()
table.clear(MapService.MapsToVote) -- Clears table
repeat
local children = MapService.Maps:GetChildren() -- gets children of folder
local map = children[math.random(#children)] -- picks a random maps
if #MapService.MapsToVote == MapService.MaxMaps then break end
-- if the Number of items in the map is equal to the Limit, If so, break
if not table.find(MapService.MapsToVote, map) then -- If map not inside table
table.insert(MapService.MapsToVote, map) -- inserts map into table
print(map.Name) -- prints map name
end
task.wait()
until #MapService.MapsToVote == MapService.MaxMaps
print(MapService.MapsToVote) -- Prints table
end
function MapService:GetVotes()
-- unfinished :(
end
return MapService -- returns table
If i dont do math.huge, it will pick the same map, the script will detect that its in the table and skip it, if so i will have less than 3 maps because the for i = 1,MapService.MaxMaps will only run 3 times
Not a lot of people look at that Category, its always Scripting Support
You can remove the found maps from the children table
local MapService = {}
MapService.Maps = game:GetService("ServerStorage").Maps
MapService.MapsToVote = {}
MapService.MaxMaps = 3
function MapService:SetVoting()
table.clear(MapService.MapsToVote)
local children = MapService.Maps:GetChildren()
for _ = 1, MapService.MaxMaps do
local map = children[math.random(#children)]
if #MapService.MapsToVote == MapService.MaxMaps then break end
if not table.find(MapService.MapsToVote, map) then
table.insert(MapService.MapsToVote, map)
table.remove(children, table.find(children, map))
print(map.Name)
end
end
print(MapService.MapsToVote) -- Prints table
end
function MapService:GetVotes()
-- unfinished :(
end
return MapService -- returns table
and if Maps folder for some reason have less than 3 objects, it would stay in an infinite loop.