It's kinda hard for me to ask the question than to explain it. So I am working on a spectating gui and the way it works is the localscript only goes up by 1. In the replicated storage there is a folder named "PlayerList" which has ObjectValues, the names of the values are a number although when a player dies they are removed from the list. This causes gaps in the PlayerList. For instance there are 3 players. So there are 3 ObjectValues inside the PlayerList folder. Player 2 dies making the folder have 2 values named "1" and "3". Now when a player ties to go from "1" to "3" my script won't work because it will try to find "2" which is nil.
Script:
local bttn = script.Parent
local currentSpec = bttn.Parent.Spectating
local playerList = game.ReplicatedStorage.Game.PlayerList
local sounds = game.ReplicatedStorage.Sounds
bttn.MouseButton1Click:Connect(function() --Clicked
if currentSpec.Value == playerList.Reserve.Value then
else
sounds.Click:Play()
currentSpec.Value = currentSpec.Value + 1 --Heres the problem.
end
end)

14 comments
what does this exactly do? im a little confused.
What it basically does is recount the items in playerlist, and relabel them. Lets say we have 1 and 3 in playerlist, this will rename it to 1 and 2. Since their are two items in playerlist
so is there something i should edit?
no, just plug it in before you add to the camspec value
im really trying to understand
sorry, put the code in before you add to current spec
bad argument #1 to 'pairs' (table expected, got Object)
woops, my bad. Re-Edited
the server would have to rename the values because if a player joins mid game it wouldnt change
Ok..then do that.
ok so "i" would be the value right?
No, i is the index
This is an unstable way to renumber things in Lua, because neither pairs nor GetChildren() guarantee that items will be handled in any particular order. So item 1 could be renumbered to 2, and 3 to 1. If you're OK with that, it will work. If you need to preserve relative order, you need to sort and manually compact an array.
Can you give any examples on the best way to do that?