Using Custom Keys / Index with Tables

Hello, I am working on a Queue System for an upcoming project I have planned. My goal is to use a Global Table to store the names of all players who have entered the queue. Here is my code for that portion of the script:

	for i,v in pairs(Seats:GetChildren()) do -- list seats
				if v.Occupant == nil then -- if seat empty
					
					character.HumanoidRootPart.CFrame = CFrame.new(v.Position + Vector3.new(0,1,0))
					character.Humanoid.JumpPower = 0 -- can't leave seat
					
					_G.PlayerList1[player.UserId] = player.Name
					
					print(player.Name .. " added to queue")
					
					print(unpack(_G.PlayerList1))
					
					break -- break the loop because we found our seat
					
				end
			end

I’ve already created a variable for the table, it’s just outside of the function. This portion of the script looks for an available seat to place the player in. But it also adds the player to the table. I am using the Player’s UserId as the key to store their name, so it’s easier to remove later. Here is the code i use to remove:

ExitQueue.OnServerEvent:Connect(function(player)
	local character = player.Character
	character.Humanoid.JumpPower = 50
	character.Humanoid.Jump = true
	wait(.25)
	character.HumanoidRootPart.CFrame = CFrame.new(Return.Position + Vector3.new(0,1,0))
	DisableUI:FireClient(player)
	
	-- remove from table
	table.remove(_G.PlayerList1, player.UserId)
	
	print(unpack(_G.PlayerList1))
	

end)

The reason for printing the tables is so I can track whos in the queue at the time. But the issue is, when I test the game, the name of the player doesn’t show up. The table is empty. I’ve tested this with multiple people in game, and same error.

This is considered one of my attemps at solving my main problem, which is removing the player from the table. Any suggestions as to why this is happening? Or an alternative method? :slight_smile:

Edit: I’ve done a lot of research on tables on the Developer Hub, and I can’t find anything to help me.

Edit2: Maybe this has something to do with trying to create a Dictionary instead of an array? I think I need an array.

1 Like

I figured it out all by myself. Turns out I can’t use table.remove, and I can’t use unpack on it either oof

3 Likes

Yeah, dictionaries can’t use either. I believe that you can work around this with a proxy table of sorts if you want to keep a dictionary format, but I’ve never seen such an example in play nor do I see the necessity of it. I think an interative loop works fine here anyway.

1 Like

Yeah I was just putting an idea together to see how it would work, came up with an easier solution.