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

Error: Table becomes nil by just becoming nil?

So I'm making a voting system for my game, and i have a WinningMinigame table, that the 1st and 2nd members are the minigame and its votes. When i print, at the end of the script, the winning minigame name and votes, in some situations it prints correctly, and sometimes it says:

 Workspace.Lobby.Votos.Voto:155: invalid argument #2 (string expected, got nil) - Server - Voto:155

I don't know why it does that becuase 1 line before it says the table members correctly:

Most voted minigame: Death corners with 1 votes. - Server - Voto:154

1 line after:

nil - Server - Voto:153   09:09:58.078 Most voted minigame: nil with nil votes. - Server - Voto:154

I dont know what's the problem, but it fires the same code twice and that glitches the code.

If u want to help me analyzing the script, it's here:

local AvMinigames = game.ServerStorage.Minigames.Images:GetChildren()
local display = script.Parent.Tela.SurfaceGui.Tela
local votesBEV = game.ReplicatedStorage.voting
local buttons = {script.Parent["1"], script.Parent["2"], script.Parent["3"]}
local PlayerLocks = {}
local VotesWithMinigames = {}
local PlayerVotes = {}
local ButTouchedEVS = {}
local displays = {}


for i, v in pairs(display:GetChildren()) do
	if v.ClassName == "ImageLabel" then
		displays[#displays + 1] = v
	end
end

local function beVisible(bool)
	if bool == true then
		display.Visible = true	
		for i, v in pairs(buttons)do
			v.SurfaceGui.Nome.Visible = true
		end
	else
		display.Visible = false
		for i, v in pairs(buttons)do
			v.SurfaceGui.Nome.Visible = false
		end
	end
end

local function endVoting()
	votesBEV:Fire(2)
end

game.Players.PlayerRemoving:Connect(function(player)
	if table.find(PlayerLocks, player) ~= nil then
		local minigame = PlayerVotes[player]

		VotesWithMinigames[minigame] = VotesWithMinigames[minigame] - 1

		display[minigame]:WaitForChild("Votos").Text = VotesWithMinigames[minigame]

		PlayerVotes[player] = nil
	end
end)

votesBEV.Event:Connect(function(caso)
	local function checkVoting()
		print("VOTOS SENDO PROCESSADOS.")
		beVisible(false)

		local MostVoted = {}
		local loop = 1

		VotesWithMinigames["Test"] = false

		for minigame, votes in pairs(VotesWithMinigames) do
			if minigame ~= "Test" then
				VotesWithMinigames["Test"] = true
				if loop == 1 then
					MostVoted = {minigame, votes}
				else
					if votes == MostVoted[2] then
						if #MostVoted == 2 then
							MostVoted[3] = minigame

							MostVoted[4] = votes

						elseif #MostVoted == 4 then
							MostVoted[5] = minigame

							MostVoted[6] = votes
						end
					elseif votes > MostVoted[2] then
						table.clear(MostVoted)

						MostVoted = {minigame, votes}

						print("Minigame ".. minigame.. " is now the most voted one, with ".. votes.. " votes.")
					end
				end
			end

			for minigame, votes in pairs(VotesWithMinigames) do
				if votes ~= false or votes ~= true then
					print(tostring("Minigame ".. tostring(minigame).." with ".. tostring(votes).." votes."))
				end

				if table.find(VotesWithMinigames, "Test") == false then
					local random = math.random(1,3)

					VotesWithMinigames["Test"] = nil

					MostVoted[1] = buttons[random].SurfaceGui.Nome.Text

					MostVoted[2] = 0

					print("Voting not done. Random minigame chosen: ".. MostVoted[1])
				end
			end

			if #MostVoted > 2 then
				if #MostVoted == 4 then
					local random = math.random(1,2)
					if random == 1 then
						table.remove(MostVoted, 3)

						table.remove(MostVoted, 4)

					else
						table.remove(MostVoted, 1)

						table.remove(MostVoted, 2)

					end
				elseif #MostVoted == 6 then
					local random = math.random(1,3)
					if random == 1 then
						table.remove(MostVoted, 3)

						table.remove(MostVoted, 4)

						table.remove(MostVoted, 5)

						table.remove(MostVoted, 6)

					elseif random == 2 then
						table.remove(MostVoted, 1)

						table.remove(MostVoted, 2)

						table.remove(MostVoted, 5)

						table.remove(MostVoted, 6)

					elseif random == 3 then
						table.remove(MostVoted, 1)

						table.remove(MostVoted, 2)

						table.remove(MostVoted, 3)

						table.remove(MostVoted, 4)

					end
				end
			end
			
			for i = 1, #MostVoted, 1 do
				print(MostVoted[i].." MostVoted; ".. table.find(MostVoted, MostVoted[i]))
			end
			print(MostVoted[1])
			print("Most voted minigame: ".. tostring(MostVoted[1]).. " with ".. tostring(MostVoted[2]).. " votes.")
			_G.minigamec = game.Workspace.Minijogos[MostVoted[1]]

			AvMinigames = game.ServerStorage.Minigames.Images:GetChildren()
			VotesWithMinigames = {}
			PlayerLocks = {}
			PlayerVotes = {}
			ButTouchedEVS = {}
			table.clear(MostVoted)
		end
	end
	
	if caso == 1 then
		
		delay(25, checkVoting)
		
		beVisible(true)
	local function checktouch(part, button)
			if part.Parent:FindFirstChild("Humanoid") then
				local character = part.Parent
				if table.find(PlayerLocks, character.Name) == nil then
					PlayerLocks[#PlayerLocks + 1] = character.Name
					
					local minigame = button.SurfaceGui.Nome.Text

					if VotesWithMinigames[minigame] == nil then

						PlayerVotes[character.Name] = minigame

						VotesWithMinigames[minigame] = 1

						display[minigame]:WaitForChild("Votos").Text = VotesWithMinigames[minigame]

					else
						PlayerVotes[character.Name] = minigame

						VotesWithMinigames[minigame] = VotesWithMinigames[minigame] + 1

						display[minigame]:WaitForChild("Votos").Text = VotesWithMinigames[minigame]
					end
				end
			end
		end


	for i, v in pairs(displays) do
		local imagenum = math.random(1, #AvMinigames)

		displays[i].Image = AvMinigames[imagenum].Image

		v.Name = AvMinigames[imagenum].Name

		script.Parent:FindFirstChild(i).SurfaceGui.Nome.Text = AvMinigames[imagenum].Name

		print("Image of minigame ".. AvMinigames[imagenum].Name.. " removed from the possibilities of minigames. ".. i)

		table.remove(AvMinigames, imagenum)

	end

	for i, v in pairs(buttons) do
		ButTouchedEVS[#ButTouchedEVS + 1] = v.Touched:Connect(function(touchedpart)
			checktouch(touchedpart, v)
		end)
	end		
	end
end)

2 comments

1

I think you should update the vote counter before printing the table. For the reason why sometimes it runs, it is because you voted before the for loop runs.

0

ok i fixed it np

1 answer

You did not label any variable as "displays", but putting it after pairs in line 152. It would have give you an warning, I think you meant "display" not "displays"?

4 comments

0

Note: Unlabeled variables value is defaulted as nil.

0

Another note: You can still run the script with warnings, but not errors.

0

This is just a guess, it is probably incorrect. You can provide more information and guesses if you want to try to solve the answer.

0

Ok so im gonna post the script, and yes i have put the variable displays.

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