Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Voting System Not Working Index Nil With Number? [closed]

So, I've been scripting a game with a user named optiver. Recently we've run into a problem wiht map voting, and here is the error:

  15:14:59.660 - ServerScriptService.MainScript:64: attempt to index nil with number
15:14:59.717 - Stack Begin
15:14:59.720 - Script 'ServerScriptService.MainScript', Line 64
15:14:59.725 - Stack End

Here are the two script handling the voting system: MainScript:

local Players = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")
local replicated = game:GetService("ReplicatedStorage")
local teams = game:GetService("Teams")
local status = replicated:WaitForChild("GameStatus")
local allWeapons = serverStorage:WaitForChild("Weapons")
local allMaps = serverStorage:WaitForChild("Maps")
local locator = workspace:WaitForChild("MapLocator")
local bindables = serverStorage:WaitForChild("Bindables")
local tallyVotes = bindables:WaitForChild("TallyVotes")
local continueScript = true
local mode = "None"

while true do
	continueScript = true
	local plrs = {}
	
	--Wait for enough players to join
	status.Value = "Waiting for players..."
	repeat
		plrs = Players:GetChildren()
		wait()
	until #plrs > 1
	
	--Intermission
	for i = 30,0,-1 do
		plrs = Players:GetChildren()
		status.Value = "Intermission: " .. i
		
		--If not enough players then go back to waiting for players
		if #plrs < 2 then
			continueScript = false
			break
		end
		
		wait(1)
	end
	
	--Do the following if there is still enough players
	if continueScript then
		status.Value = "Time to vote! Vote using the voting pads!"
		mode = "Skirmish"
		
		wait(5)
		
		if mode == "Skirmish" or mode == "Teams" then
			local maps = allMaps:GetChildren()
			local mapsToVote = {}
			
			for i = 1,2,1 do
				local chosenIndex = math.random(1,#maps)
				local chosenMap = maps[chosenIndex]
				print("Got maps to vote for")
				
				table.insert(mapsToVote,chosenMap)
				table.remove(maps,chosenIndex)
			end
			print("Maps ready")
			
			local votes = tallyVotes:Invoke(mapsToVote)
			print("Finished invoke!")
			local chosenMap
			
			if #votes[1][1] > #votes[2][1] then
				chosenMap = votes[1][2]
			elseif  #votes[1][1] < #votes[2][1] then
				local chosenMap = votes[2][2]
			else
				local randomNum = math.random(1,3)
				
				chosenMap = votes[randomNum][2]
			end
			
			--local chosenMap = maps[math.random(1,#maps)]
			local map = chosenMap:Clone()
			local spawns = map.Spawns:GetChildren()
			plrs = Players:GetChildren()
		
			--Get map into position
			map.Parent = workspace
			map:SetPrimaryPartCFrame(CFrame.new(locator.Position))
		
			--Give players swords
			for i, player in pairs (plrs) do
				if player.Character then
					--Give player a sword
					local weapon = allWeapons.ClassicSword
					weapon:Clone().Parent = player.Backpack
				
					--Now teleport them to the map
					local spawnIndex = math.random(1,#spawns)
					local spawnLoc = spawns[spawnIndex]
				
					player.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(spawnLoc.Position + Vector3.new(0,10,0))
					--Make sure they don't spawn in the same spot
					table.remove(spawns,spawnIndex)
				
					--Give a tag so we can tell they are alive
					local tag = Instance.new("BoolValue",player.Character)
					tag.Name = "Alive"
				else
					table.remove(plrs,i)
				end
			end
	
			local endStatus = "TimeUp"
			print("Game has started!")
	
			for timeLeft = 120,0,-1 do
				for i, player in pairs (plrs) do
					if player.Character then
						--Check for our tag
						if player.Character:FindFirstChild("Alive") then
							--Do nothing
						else
							print(player.Name .. " has died!")
							table.remove(plrs,i)
						end
					else
						print(player.Name .. " has died!")
						table.remove(plrs,i)
					end
				end
			
				if #plrs == 1 then
					endStatus = "FoundWinner"
					break
				elseif #plrs < 1 then
					endStatus = "None"
					break
				end
			
				status.Value = "There are " .. timeLeft .. " seconds left in the game!"
				wait(1)
			end
	
			if endStatus == "None" then
				status.Value = "Nobody won! OOF"
			elseif endStatus == "FoundWinner" then
				status.Value = plrs[1].Name .. " won the match!"
			
				wait(5)
			
				plrs[1].leaderstats.Points.Value = plrs[1].leaderstats.Points.Value + 10
				plrs[1].leaderstats.Wins.Value = plrs[1].leaderstats.Wins.Value + 1
				plrs[1].Character.Alive:Destroy()
			
				if plrs[1].leaderstats.Wins.Value == 5 then
					plrs[1].Stats.Rank.Value = "Novice"
				elseif plrs[1].leaderstats.Wins.Value == 10 then
					plrs[1].Stats.Rank.Value = "Amateur"
				elseif plrs[1].leaderstats.Wins.Value == 20 then
					plrs[1].Stats.Rank.Value = "Ok"
				elseif plrs[1].leaderstats.Wins.Value == 30 then
					plrs[1].Stats.Rank.Value = "Good"
				elseif plrs[1].leaderstats.Wins.Value == 40 then
					plrs[1].Stats.Rank.Value = "Decent"
				elseif plrs[1].leaderstats.Wins.Value == 50 then
					plrs[1].Stats.Rank.Value = "Epic"
				elseif plrs[1].Stats.Rank.Value == 100 then
					plrs[1].Stats.Rank.Value = "Amazing"
				elseif plrs[1].leaderstats.Wins.Value == 150 then
					plrs[1].Stats.Rank.Value = "Godly"
				elseif plrs[1].leaderstats.Wins.Value == 200 then
					plrs[1].Stats.Rank.Value = "Unbelievable"
				elseif plrs[1].leaderstats.Wins.Value == 250 then
					plrs[1].Stats.Rank.Value = "Lord Of Swords"
				elseif plrs[1].leaderstats.Wins.Value == 300 then
					plrs[1].Stats.Rank.Value = "The Best"
				end
			
				plrs[1]:LoadCharacter()
			else
				status.Value = "Time ran out!"
			
				for i, player in pairs (plrs) do
					plrs[1].Character:FindFirstChild("Alive"):Destroy()
					plrs[1].Backpack:FindFirstChild("ClassicSword"):Destroy()
					plrs[1]:LoadCharacter()
				end
			end
		
			--Reset the game
			map:Destroy()
		elseif mode == "Teams" then
			local teamMaps = serverStorage.TeamMaps:GetChildren()
			local chosenMap = teamMaps[math.random(1,#teamMaps)]
			local map = chosenMap:Clone()
			local redSpawns = map.Spawns.RedSpawns:GetChildren()
			local blueSpawns = map.Spawns.BlueSpawns:GetChildren()
			
			map.Parent = workspace
			map:SetPrimaryPartCFrame(CFrame.new(locator.Position))
			
			local redTeam = Instance.new("Team",teams)
			--redTeam.TeamColor = 
		else
			error("There has been no gamemode selected. Please examine the code.")
		end
	else
		--Do nothing
	end
end

TallyVotes:

local serverStorage = game:GetService("ServerStorage")
local bindables = serverStorage:WaitForChild("Bindables")
local tallyVotes = bindables:WaitForChild("TallyVotes")

tallyVotes.OnInvoke = function(mapsToVote)
	local map1 = {{},mapsToVote[1]}
	local map2 = {{},mapsToVote[2]}
	
	workspace.Part1Gui.SurfaceGui.TextLabel.Text = mapsToVote[1].Name
	workspace.Part2Gui.SurfaceGui.TextLabel.Text = mapsToVote[2].Name
		
	for i = 0,300,1 do
		script.Parent.Part1.Touched:Connect(function(part)
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			local canVote = true
			
			if humanoid then
				for i, player in pairs (map1[1]) do
					if player.Name == part.Parent.Name then
						canVote = false
					end
				end
				
				for i, player in pairs (map2[1]) do
					if player.Name == part.Parent.Name then
						table.remove(map2[1],i)
					end
				end
				
				
				if canVote then
					table.insert(map1[1],i)
				end
			end
		end)
		
		script.Parent.Part2.Touched:Connect(function(part)
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			local canVote = true
			
			if humanoid then
				for i, player in pairs (map2[1]) do
					if player.Name == part.Parent.Name then
						canVote = false
					end
				end
				
				for i,player in pairs (map1[1]) do
					if player.Name == part.Parent.Name then
						table.remove(map1[1],i)
					end
				end
				
				if canVote then
					table.insert(map2[1],i)
				end
			end
		end)
		
		wait(0.01)
	end
	
	print(#map1[1])
	
	local votes = {
		Teams = map1;
		Skirmish = map2
	}
	
	return votes
end

1 answer

Hey!

It looks like on line 64, you're trying to do #votes[1][1] which isn't really possible since you're trying to index a number like it's a table. Instead, try doing votes[1][1] and see if that works. Let me know what happens.

If this helped, please remember to mark as answered and upvote!

1 comment

0

Did not work. Still giving me same error as before.

This discussion is preserved as it was. New answers, comments, and votes are closed.