Repeating Table Issue

Hey there! I’m trying to make a game with minigames, yet I keep repeating the second game in the queue. Example: When I first start the game, it starts with the game walkingBombs, then after that game is done, it will go onto fallingBlocks. After fallingBlocks, it will just repeat that same minigame over and over again forever and will not go onto the last game in the queue.

I’ve looked around and I think the error is somewhere with the table. Maybe it’s because I am removing an item from a table and then looking for that item in the wrong spot, but I’m not the best at tables so I didn’t know how to debug it.

local gameQueue = {"walkingBombs", "fallingBlocks", "walkingBombs"}

local minigameName
local minigameNumber = 1

game.Workspace.Variables.GameEnded.Changed:Connect(function()
	if game.Workspace.Variables.GameEnded.Value == true then
		game.Workspace.Variables.GameEnded.Value = false
		for number, minigame in ipairs(gameQueue) do
			minigameName = minigame
			print("Next game: " .. tostring(minigame))
			table.remove(gameQueue, tonumber(minigameNumber))
			break
		end
		minigameNumber = tonumber(minigameNumber) + 1
		if minigameName == "fallingBlocks" then
        --Starts game
		elseif minigameName == "walkingBombs" then
		--Starts game	
		end
	end
end)

Thank you in advance! :smiley:

I do see something with this, I don’t believe you’d need to even change the value of minigameNumber using a tonumber string format, since minigameNumber will always be a number value starting at 1

Maybe you meant to put number instead? :thinking:

1 Like

That fixed it! Thank you so much for your help!!

:smiley:

1 Like

Hello mason! In situations like these where you want to iterate through a table until it reaches the end and then loops back to the beginning, it’s best to keep track of what position of the table we’re currently in, as you had correctly done (using minigameNumber).

However, it’s best not to alter anything in the table! Your game was looping back to the first game because you removed all the possible game options in the for loop, and thus the minigameName was forever set to the last entry in the table.

In the example below, I’ve removed the last string value in the table (so now it only contains the two possible game types) and simply adds 1 to the minigameNumber every time it runs. If the number is ever the same as the length of the table (#queue, in this case 2), it’ll reset the number back to 1. And then I simply index the table with the current minigameNumber to get the minigameName at that position!

Hope this helps!

local gameQueue = {"walkingBombs", "fallingBlocks"} --Only need to mention each one once!

local minigameName
local minigameNumber = 1

game.Workspace.Variables.GameEnded.Changed:Connect(function()
	if game.Workspace.Variables.GameEnded.Value == true then
		game.Workspace.Variables.GameEnded.Value = false
		minigameName = gameQueue[minigameNumber] --Current minigame
		minigameNumber += 1 --Jumps to the next minigame position
		if minigameName == "fallingBlocks" then
            --Starts game
		elseif minigameName == "walkingBombs" then
		    --Starts game	
		end
        if minigameNumber == #gameQueue then --Check if the position is at end of gameQueue's length
            minigameNumber = 1 --Resets the minigame number
        end
	end
end)