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)
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
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)
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
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)
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)