In a localscript.
local plr = game.Players.LocalPlayer repeat wait() until plr.Character local char = plr.Character table.insert(_G.PlayersInQueue,plr.Name) game.Players.PlayerRemoving:connect(function(player) if player.Name == plr.Name then table.remove(_G.PlayersInQueue,plr.Name) end end)
In a serverscript.
_G.PlayersInQueue = {}
You should really be more specific about your question. However, from the code you've provided, I see the common (but false) assumption you're making about table.remove
. The remove
function is meant for arrays {1, 2, 3, ...}
rather than dictionaries {key = value, ...}
. This function will not traverse a table looking for a given value, instead, it will search for a given index. Here's a quick example:
table.remove({"a", "b", "c"}, 2)
The snippet above passes an array with three elements (a, b, and c) to table.remove
, and requests to remove the second position in the table. This is what the 2
represents as the second argument -- the position in the array to be removed and replaced by the next element (if there is one). If you seek a function that removes an entry from an array depending on values, then you must traverse the array and compare all of them...
local function removeEntryFromArray(array, entry) for i = 1, #array do if array[i] == entry then table.remove(array, i) end end end removeEntryFromArray({"a", "b", "c"}, "b")
This is called a linear search, opposed to other searching algorithms such as a binary search. This, however, is beyond the scope of what we're talking about, so I'll leave it to you to further investigate that.
The code above accomplishes the same thing as previous example, except this time, we're choosing the entry to be removed by it's value. Notice how we're still using table.remove
in the function. This is because whenever you're removing a value from an array, you want the table to remain an array. Let me explain: If we just set a random index equal to nil in an array, the numeric indices of the table are no longer consecutive (such as 1, 2, 3, and so on). Therefore, it's no longer an array.
local t = {"a", "b", "c"} t[2] = nil
If we used the code above in place of our removeEntryFromArray
function, we would run into some problems. As I mentioned, table t
is now no longer an array. Now it looks like this: t = {"a", nil, "b"}
. Not good. Now you lose the ability to accurately get the length of the table with the #
operator, or the table.getn
function. This means you'll run into problems traversing it with a numeric for loop, since you will only iterate over the "array" part of the table.
Good question. Internally, table.remove
function does set the index to nil. However, it also updates all the indices in the array after the index it sent to nil and pushes them down the list to fill the gap it created. Thus, the array remains an array since all indices are in sequential order.
Locked by User#24403
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?