How would I improve this Vote Setup system?

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

Why math.huge?

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

repeat better, and this should be in #help-and-feedback:code-review :wink:

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.

1 Like

Once again, since MapService.MaxMaps is equal to 3, it will only run 3 times, it leads to me having less than 3 maps

I’m Currently testing with 6 Folders:
Screenshot (116)

(I didnt see the table.remove)
I guess that will work, thanks :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.