How to make a function on player removing and [1] in table

Hello there,
I am trying to make a function if someone leaves from the game, while being in a table. And if the player is first in the table, then something happens.

I already have a script with a on player removing function, but i don’t know how to make it.

I tried to figure it out by myself, but i was unable to solve the problem.

For example, someone’s in table at position 1, then leaves or disconnect the server. I wan’t some function if the player is at position 1 and leaves the server. How can i make it?

-- Code i've trying to fix
game.Players.PlayerRemoving:Connect(function(plr)
	local player = table.find(queue, plr)
	if player == nil or player == -1 or player == 0 then 
		warn("Player not in table!") -- the player is not in the table
	elseif table.find(queue[1], plr) then -- if player is in queue position 1 and leaves the game
		print("Worked")
	end
end)
1 Like

Why do you need Queue[1]? Is there a table inside a table?

3 Likes

I need the Queue[1], because if the player is in queue position 1, then the player will be removed(i know how to make it), but it also moveto next player in queue

Pretty sure that’s not how it works, please give us the full script for context.

1 Like

and

are two different scripts. Please tell us which is the one that’s not working.

game.Players.PlayerRemoving:Connect(function(plr)
	local player = table.find(queue, plr)
	if player == nil or player == -1 or player == 0 then 
		warn("Player not in table!") -- the player is not in the table
	elseif table.find(queue[1], plr) then -- if player is in queue position 1 and leaves the game
		print("Worked")
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	local player = table.find(queue, plr)
	if player == nil or player == -1 or player == 0 then 
		warn("Player not in table!") -- the player is not in the table
	else
        
		table.remove(queue, player) -- if player is in queue position 1 and leaves the game
		print("Worked")
	end
end)

Try this.
Wait. I just realised…

If the player leaves but he has to be in [1] queue then something happens.(some function)

This returns an index of the table.

table.find(queue, plr)

so basically

local index = table.find(queue, plr)
if index == 1 then 
	print("Worked...");
else
	warn("Not first in the table!")
end
2 Likes

To check if the player is The First in the Table, you can use :

local count=0
for i, v in pairs(yourtable) do  -- Change 'yourtable' to the table 
   count=count+1
   if v==Plr then
      if count==1 then 
         -- Player is first in table
         -- Do stuff
         break -- Break Loop Because we found What we needed
      end
   end
end
1 Like

There’s no need for that, you can just use table.find

table.find can be used to check if the player in in the table, but since the player is an instance and not a string im not sure.

(both solutions will work, im sure table.find() will be a much simpler solution but this is the best i got.)

Try this

game.Players.PlayerRemoving:Connect(function(plr)
	local player = table.find(queue, plr)
	if player == nil or player == -1 or player == 0 then 
		warn("Player not in table!") -- the player is not in the table
	elseif queue[1] == plr then -- if player is in queue position 1 and leaves the game
		table.remove(queue, 1)
		print("Worked")
	end
end)
1 Like

if the table is empty, the queue[1] will error.
I suggest adding an if statment to check if the table has values. (if #queue>0 then)

It won’t error, it just won’t output anything.

1 Like

yeah, my bad.
Sorry for the confusion

local Game = game
local Players = Game:GetService("Players")

local Array = {}

local function OnPlayerAdded(Player)
	table.insert(Array, Player)
end

local function OnPlayerRemoving(Player)
	local Index = table.remove(Array, Player)
	if Index == 1 then
		--Do code.
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)