Error: Table becomes nil by just becoming nil?
Asked by
4 years ago Edited 4 years ago
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:
001 | local AvMinigames = game.ServerStorage.Minigames.Images:GetChildren() |
002 | local display = script.Parent.Tela.SurfaceGui.Tela |
003 | local votesBEV = game.ReplicatedStorage.voting |
004 | local buttons = { script.Parent [ "1" ] , script.Parent [ "2" ] , script.Parent [ "3" ] } |
005 | local PlayerLocks = { } |
006 | local VotesWithMinigames = { } |
007 | local PlayerVotes = { } |
008 | local ButTouchedEVS = { } |
012 | for i, v in pairs (display:GetChildren()) do |
013 | if v.ClassName = = "ImageLabel" then |
014 | displays [ #displays + 1 ] = v |
018 | local function beVisible(bool) |
020 | display.Visible = true |
021 | for i, v in pairs (buttons) do |
022 | v.SurfaceGui.Nome.Visible = true |
025 | display.Visible = false |
026 | for i, v in pairs (buttons) do |
027 | v.SurfaceGui.Nome.Visible = false |
032 | local function endVoting() |
036 | game.Players.PlayerRemoving:Connect( function (player) |
037 | if table.find(PlayerLocks, player) ~ = nil then |
038 | local minigame = PlayerVotes [ player ] |
040 | VotesWithMinigames [ minigame ] = VotesWithMinigames [ minigame ] - 1 |
042 | display [ minigame ] :WaitForChild( "Votos" ).Text = VotesWithMinigames [ minigame ] |
044 | PlayerVotes [ player ] = nil |
048 | votesBEV.Event:Connect( function (caso) |
049 | local function checkVoting() |
050 | print ( "VOTOS SENDO PROCESSADOS." ) |
056 | VotesWithMinigames [ "Test" ] = false |
058 | for minigame, votes in pairs (VotesWithMinigames) do |
059 | if minigame ~ = "Test" then |
060 | VotesWithMinigames [ "Test" ] = true |
062 | MostVoted = { minigame, votes } |
064 | if votes = = MostVoted [ 2 ] then |
065 | if #MostVoted = = 2 then |
066 | MostVoted [ 3 ] = minigame |
070 | elseif #MostVoted = = 4 then |
071 | MostVoted [ 5 ] = minigame |
075 | elseif votes > MostVoted [ 2 ] then |
076 | table.clear(MostVoted) |
078 | MostVoted = { minigame, votes } |
080 | print ( "Minigame " .. minigame.. " is now the most voted one, with " .. votes.. " votes." ) |
085 | for minigame, votes in pairs (VotesWithMinigames) do |
086 | if votes ~ = false or votes ~ = true then |
087 | print ( tostring ( "Minigame " .. tostring (minigame).. " with " .. tostring (votes).. " votes." )) |
090 | if table.find(VotesWithMinigames, "Test" ) = = false then |
091 | local random = math.random( 1 , 3 ) |
093 | VotesWithMinigames [ "Test" ] = nil |
095 | MostVoted [ 1 ] = buttons [ random ] .SurfaceGui.Nome.Text |
099 | print ( "Voting not done. Random minigame chosen: " .. MostVoted [ 1 ] ) |
103 | if #MostVoted > 2 then |
104 | if #MostVoted = = 4 then |
105 | local random = math.random( 1 , 2 ) |
107 | table.remove(MostVoted, 3 ) |
109 | table.remove(MostVoted, 4 ) |
112 | table.remove(MostVoted, 1 ) |
114 | table.remove(MostVoted, 2 ) |
117 | elseif #MostVoted = = 6 then |
118 | local random = math.random( 1 , 3 ) |
120 | table.remove(MostVoted, 3 ) |
122 | table.remove(MostVoted, 4 ) |
124 | table.remove(MostVoted, 5 ) |
126 | table.remove(MostVoted, 6 ) |
128 | elseif random = = 2 then |
129 | table.remove(MostVoted, 1 ) |
131 | table.remove(MostVoted, 2 ) |
133 | table.remove(MostVoted, 5 ) |
135 | table.remove(MostVoted, 6 ) |
137 | elseif random = = 3 then |
138 | table.remove(MostVoted, 1 ) |
140 | table.remove(MostVoted, 2 ) |
142 | table.remove(MostVoted, 3 ) |
144 | table.remove(MostVoted, 4 ) |
150 | for i = 1 , #MostVoted, 1 do |
151 | print (MostVoted [ i ] .. " MostVoted; " .. table.find(MostVoted, MostVoted [ i ] )) |
154 | print ( "Most voted minigame: " .. tostring (MostVoted [ 1 ] ).. " with " .. tostring (MostVoted [ 2 ] ).. " votes." ) |
155 | _G.minigamec = game.Workspace.Minijogos [ MostVoted [ 1 ] ] |
157 | AvMinigames = game.ServerStorage.Minigames.Images:GetChildren() |
158 | VotesWithMinigames = { } |
162 | table.clear(MostVoted) |
168 | delay( 25 , checkVoting) |
171 | local function checktouch(part, button) |
172 | if part.Parent:FindFirstChild( "Humanoid" ) then |
173 | local character = part.Parent |
174 | if table.find(PlayerLocks, character.Name) = = nil then |
175 | PlayerLocks [ #PlayerLocks + 1 ] = character.Name |
177 | local minigame = button.SurfaceGui.Nome.Text |
179 | if VotesWithMinigames [ minigame ] = = nil then |
181 | PlayerVotes [ character.Name ] = minigame |
183 | VotesWithMinigames [ minigame ] = 1 |
185 | display [ minigame ] :WaitForChild( "Votos" ).Text = VotesWithMinigames [ minigame ] |
188 | PlayerVotes [ character.Name ] = minigame |
190 | VotesWithMinigames [ minigame ] = VotesWithMinigames [ minigame ] + 1 |
192 | display [ minigame ] :WaitForChild( "Votos" ).Text = VotesWithMinigames [ minigame ] |
199 | for i, v in pairs (displays) do |
200 | local imagenum = math.random( 1 , #AvMinigames) |
202 | displays [ i ] .Image = AvMinigames [ imagenum ] .Image |
204 | v.Name = AvMinigames [ imagenum ] .Name |
206 | script.Parent:FindFirstChild(i).SurfaceGui.Nome.Text = AvMinigames [ imagenum ] .Name |
208 | print ( "Image of minigame " .. AvMinigames [ imagenum ] .Name.. " removed from the possibilities of minigames. " .. i) |
210 | table.remove(AvMinigames, imagenum) |
214 | for i, v in pairs (buttons) do |
215 | ButTouchedEVS [ #ButTouchedEVS + 1 ] = v.Touched:Connect( function (touchedpart) |
216 | checktouch(touchedpart, v) |