Invalid argument #2 to 'random' (interval is empty)

local module = {}

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local status = ReplicatedStorage.Values.Status
--[[
local mapVotes = {
	{order = 1;name = "Grass";players = {}},
	{order = 2;name = "Snow";players = {}},
	{order = 3;name = "Desert";players = {}}
}
]]
local mapVotes = {}

for i ,item in ipairs(ReplicatedStorage:FindFirstChild("Maps"):GetChildren()) do
	if item then
		item.Parent = nil
		table.insert(mapVotes, item)
	end
end

local function getMap()
	local randomMap = mapVotes[math.random(1, #mapVotes)]

	-- if map exists in the mapVotes table
	for i,map in pairs(mapVotes) do
		if map.name == randomMap.Name then
			return getMap()
		end
	end

	return randomMap.Name
end

function module.MapVoting(dur)
	-- Start the voting system
	mapVotes = {}

	for i = 1,3 do
		table.insert(mapVotes, {order = i; name = getMap(); players = {};})
	end

	ReplicatedStorage.Events.UpdateVotingSystem:FireAllClients(mapVotes)

	ReplicatedStorage.Values.VotingProgress.Value = true

	local placeVoteConnection = ReplicatedStorage.Events.PlaceVote.OnServerEvent:Connect(function(player,voteNumber)

		-- Check if already voted
		for i,map in pairs(mapVotes) do
			for x,plr in pairs(map.players) do
				if plr == player.UserId then
					table.remove(map.players, x)
					break
				end
			end
		end

		-- Place Vote
		for i,map in pairs(mapVotes) do
			if map.order == voteNumber then
				table.insert(map.players, player.UserId)
			end
		end

		ReplicatedStorage.Events.UpdateVotingSystem:FireAllClients(mapVotes)
	end)

	for i = dur,0,-1 do
		status.Value = "You have ".. i .." "..((i==1 and "second") or "seconds").." to vote!"
		task.wait(1)
	end

	-- End the voting system
	placeVoteConnection:Disconnect()

	ReplicatedStorage.Values.VotingProgress.Value = false

	-- Get the winner and reveal
	table.sort(mapVotes, function(a,b) return #a.players > #b.players end)

	local winner

	-- If the all pads have the same vote
	if #mapVotes[1].players == #mapVotes[2].players and #mapVotes[2].players == #mapVotes[3].players then
		winner = mapVotes[math.random(1,3)]
		-- If 2 pads have the same vote
	elseif #mapVotes[1].players == #mapVotes[2].players then
		winner = mapVotes[math.random(1,2)]
	else
		winner = mapVotes[1]
	end

	status.Value = winner.name.." was chosen!"

	return ReplicatedStorage.Maps:FindFirstChild(winner.name)
end

return module

ServerScriptService.Main.Voting:23
local randomMap = mapVotes[math.random(1, #mapVotes)]

The error usually occurs when the second parameter is lower than the first parameter - so #mapVotes is 0. I’ll see if I can identify why, although it may just be because there is no one voting

1 Like

The map votes table is empty. The error was thrown because you tried math.random(1, 0). If the 2nd argument is less than the first, it throws this error.

1 Like
local randomMap = mapVotes[math.random(1, math.max(1,#mapVotes))]
local module = {}

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local status = ReplicatedStorage.Values.Status

local mapVotes = {}

for i ,item in ipairs(ReplicatedStorage:FindFirstChild("Maps"):GetChildren()) do
	if item then
		item.Parent = nil
		table.insert(mapVotes, item)
	end
end

local function getMap()
	local randomMap = mapVotes[math.random(1, #mapVotes)]

	-- if map exists in the mapVotes table
	for i,map in pairs(mapVotes) do
		if map.name == randomMap.Name then
			return randomMap.Name	
		end
	end
end

function module.MapVoting(dur)
	-- Start the voting system
	local mapVoteings = {}
	
	for i = 1,3 do
		table.insert(mapVoteings, {order = i; name = getMap(); players = {};})
	end
	
	ReplicatedStorage.Events.UpdateVotingSystem:FireAllClients(mapVoteings)

	ReplicatedStorage.Values.VotingProgress.Value = true

	local placeVoteConnection = ReplicatedStorage.Events.PlaceVote.OnServerEvent:Connect(function(player,voteNumber)

		-- Check if already voted
		for i,map in pairs(mapVoteings) do
			for x,plr in pairs(map.players) do
				if plr == player.UserId then
					table.remove(map.players, x)
					break
				end
			end
		end

		-- Place Vote
		for i,map in pairs(mapVoteings) do
			if map.order == voteNumber then
				table.insert(map.players, player.UserId)
			end
		end

		ReplicatedStorage.Events.UpdateVotingSystem:FireAllClients(mapVoteings)
	end)

	for i = dur,0,-1 do
		status.Value = "You have ".. i .." "..((i==1 and "second") or "seconds").." to vote!"
		task.wait(1)
	end

	-- End the voting system
	placeVoteConnection:Disconnect()

	ReplicatedStorage.Values.VotingProgress.Value = false

	-- Get the winner and reveal
	table.sort(mapVoteings, function(a,b) return #a.players > #b.players end)

	local winner

	-- If the all pads have the same vote
	if #mapVoteings[1].players == #mapVoteings[2].players and #mapVoteings[2].players == #mapVoteings[3].players then
		winner = mapVoteings[math.random(1,3)]
		-- If 2 pads have the same vote
	elseif #mapVoteings[1].players == #mapVoteings[2].players then
		winner = mapVoteings[math.random(1,2)]
	else
		winner = mapVoteings[1]
	end

	status.Value = winner.name.." was chosen!"


	for i ,item in ipairs(mapVotes) do
		if item.name == winner.name then
			return item
		end
	end
end

return module

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