Table not shifting upon removing a value

I am trying to make the table shift (move the other values up) when I remove a value, however its not working.
I have tried making the value stored a string instead of instance but it didn’t fix the problem.

My code:

local inLine = {}
-- When you enter the line --
script.Parent.Start.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
			local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			if table.find(inLine,plr.Name) then
				print("Already in line. Ignoring.")
			else
				if #inLine == 8 then
					print("Max amount of players in line!")
				else
					if plr:FindFirstChild("BypassStart") then
						return
					else
						table.insert(inLine,plr.Name)
						local amtInLine = #inLine
						controlGui("give",plr)
						while wait(.01) do
							if table.find(inLine,plr.Name) then
								-- To keep the player's position up-to-date :)
								local plrsPlaceInLine = table.find(inLine,plr.Name)

								plr.Character.Humanoid:MoveTo(script.Parent.Spots:FindFirstChild(plrsPlaceInLine).Position)
							end				
						end
					end
				end
			end
		end
	end
end)

-- When someone lets you out of the line --
script.Parent.End.ClickDetector.MouseClick:Connect(function(plr)
	if plr:GetRankInGroup(groupID) >= rankID then
		local nextPlayer = inLine[1]
		local playerInstance =  game.Players:FindFirstChild(nextPlayer)
		print(inLine[1])
		print(inLine[2])
		local bypassStart = Instance.new("BoolValue")
		bypassStart.Name = "BypassStart"
		bypassStart.Parent = playerInstance
		bypassStart.Value = true
		playerInstance.Character.Humanoid:MoveTo(script.Parent.End.Position)
		local int = table.find(inLine,nextPlayer)
		table.remove(inLine,int)
		controlGui("remove",game.Players:FindFirstChild(nextPlayer))
		wait(1)
		plr["BypassStart"]:Destroy()
	end
end)

When I try to print playerInstance it apparently doesn’t exist (argument 1 missing or nil) which would mean that nextPlayer doesn’t exist which would mean the 1st value in the table is nil which would mean the table is not shifting how it should.

Why not use table.remove? It will shift all entries downward after the index you want to remove.

local t = {1;2;3;4} 
print(t) -- 1, 2, 3, 4
table.remove(t, 3) 
print(t) -- 1, 2, 4

He uses table.remove if you scroll down in his script.

There doesn’t seem to be any issues in the code, but it could be that some other part of the script is modifying the values in inLine. Also, obviously make sure there are at least 2 players in the line before you click the button.

There was 2 players, then i clicked it and it worked, then there was 1 player, i tried again and it didnt work (1 player left in line)

Bumpity bump bump. Still not fixed. :frowning:

Have you tried sorting it? Something like:

local Table = {
[1] = {
Amount = 0 -- example stat!
},
[2] = {
Amount = 1 -- example stat!
},
[3] = {
Amount = 2 -- example stat!
},
}

table.remove(Table, 2);
table.sort(Table, function(a1, a2)
return a2.Amount > a1.Amount;
end)

So for my script would it be (this is written as if I am in line)

local inLine = {
"phyouthcenter1"
}

local int = table.find(inLine,nextPlayer)
table.remove(inLine,int)
table.sort(inLine, function(a1, a2))

Btw, what is the function for?

The function is the sorting method! In this case, I was sorting it by the lowest amount! You could use a table and have it represent the index, and then the player so [1] = {{player = “phyouthcenter1”, position = 1}}, then you can sort it by position, you’d have to adjust the way the rest of the function works though.

Lol I am so confused right now. :skull: