How to get a player out of table

local RS = game:GetService("ReplicatedStorage")

local Queue = RS.Events.Queue

local WaitingQuenue = {}
local InQuenue = {}

Queue.OnServerEvent:Connect(function(Player)
	print("Added Player")
	table.insert(WaitingQuenue ,Player)

	print(WaitingQuenue[1])
	
	if #WaitingQuenue >= 2 then
		if #InQuenue > 2 then return end
		print("Added to New Quenue")
		table.remove(WaitingQuenue, Player)
		table.insert(InQuenue, Player)
	end
	
	 
end)

I want to remove a player out of one table to another table. How would I easily do this?
Line - table.remove(WaitingQuenue, Player)
I want to remove a player from one table to NewQuenue.

Since you used table.insert then you can utilize table.find to get the index and then remove it that way.

local exists = table.find(WaitingQuenue, Player)
if exists then
    table.remove(WaitingQuenue, exists)
end

But there is something else to note with your code, if you just updated that to remove it will only put the last player joining the queue into the InQuenue table, leaving the first queue person in the waiting queue. Instead you would want to create another function which itterates through your waitingqueue table, clears them out and puts them into the new table.

You can also try this method…

local function MoveToInQueue()
    for _, v in ipairs(WaitingQuenue) do
        table.insert(InQuenue, v)
       v = nil
    end
end
4 Likes

You need to do table.remove(WaitingQuenue, arrayvalue), lets say I had a table like this:

local yes = {"Apple", "boo"}
local no = {}

print(yes[1]) -- would print "Apple"
print(yes[2]) -- would print "boo"

I would like to remove “apple” from the table variable, yes, and insert it into the other table.

local yes = {"Apple", "boo"}
local no = {}
wait(1)
table.remove(yes, 1) -- would remove the first array, apple, from "yes"
table.insert(no, "Apple") -- inserts it into the new table

you can also do tablename[array] = nil, but the other method is easier.

1 Like

table.remove expects the position you want removed and not the value so you will have to search.

Example:

function removeFirst(tbl, val)
  for i, v in ipairs(tbl) do
    if v == val then
      return table.remove(tbl, i)
    end
  end
end

removeFirst(myTable, 'string1')
2 Likes

Yeah I just tried it the first one worked. Thank you so much.

Well, it depends on if you know the array value or not. You can search all of it with in pairs (@SnazpantsMixer explains it)

It doesn’t depend, you need to search for it.

Yes, so did I.
https://www.lua.org/pil/19.2.html

1 Like

What do you mean? If the array only has one value, you can just do table[1], if you know how many values it has then it depends.

Yes, that is IF it only has one value.
In most cases it depends, hence: you need to search.

1 Like