Ok So im trying to remove a player from a table for playing a game if they are still watching an Intro.
Heres the Code:
local Player = game.Players.LocalPlayer
local plrs = {}
for i, player in pairs (game.Players:GetPlayers()) do
if player then
table.insert(plrs,player)--Add each players into plrs table
print(player)
elseif Player.IntroTag.Value == true then
table.remove(plrs,player) --Remove Player from table if they are in the intro
warn(player,', was Removed From Table Because They are in the Intro.')
end
end
The array and the position of the element you want to remove. You need the element position in the array to remove it.
To do that you do a for loop and check each element in the table until you find the one you want to remove.
for i = 1, #plrs do
if plrs[i] == "value you want to remove" then
table.remove(plrs, i)
end
end
-- #ArrayName > Returns the number of elements in the array.
Each time one loop is finished, i will increase by 1.
Well, no.
If you do that you’re trying to remove the Boolean value from the table, not the player.
You have to do a simple check before removing the player from the table to see if the Boolean value is true.
It should look something like this:
local Player = game.Players.LocalPlayer
local Players = {}
for _, v in pairs(game.Players:GetPlayers()) do
if v then
table.insert(Players, Player)
print(Player.Name .."has been added to the array!")
elseif Player.IntroTag.Value then
for i = 1, #Players do
if Players[i] == Player then
table.remove(Players, i)
print(Player.Name .."has been removed from the array!")
end
end
end
end
Each element in an array has it’s own number position.
For example:
local Admins = {"JakyeRU", "iSkepticalBoi", "Roblox"}
--[[ Admins[1] is JakyeRU.
Admins[2] is iSkepticalBoi.
Admins[3] is Roblox. --]]
Dosent Remove me from table and dosent print the warn.
local plrs = {}
for i, player in pairs (game.Players:GetPlayers()) do
if player then
table.insert(plrs,player)--Add each players into plrs table
print(player)
elseif Player.IntroTag.Value == true then
for i = 1, #plrs do
if plrs[i] == Player then
table.remove(plrs,i)--Remove Player from table if they are in the intro
warn(Player.Name..', was Removed From Table Because They are in the Intro.')
end
end
end
end
I think you should lowercase the ‘Player’ there, so it matches with the other. That might be what’s causing it not to work right now. There’s also another one of those on the warning line.
LUA is case sensitive.
That means Player and player are 2 different things.
You should change elseif Player.IntroTag.Value to elseif player.IntroTag.Value and if plrs[i] == Player to if plrs[i] == player.
Your logic is not correct. Check player.IntroTag.Value in the first if block to see if a player is in the intro or not in the intro. Simply use an else for the case where Value is false instead of an elseif.
You could add a check before you add someone to the array.
For example:
local Player = game.Players.LocalPlayer
local Players = {}
for _, v in pairs(game.Players:GetPlayers()) do
if v.IntroTag.Value then return end
table.insert(Players, Player)
end
The return will stop the code. So if the IntroTag is true, the code will stop and the player will not be added to the Array Players.
As @PeZsmistic mentioned, as you’re calling a table of :GetPlayers(), you’ll only get players. Therefore, the elseif statement won’t run.
If you needed to differentiate the players’ values, then add the following clause:
local plrs = {}
for i, player in pairs (game.Players:GetPlayers()) do
if player.IntroTag.Value == false then --Checking the player from before was redundant, as it returns only the children of a table of players.
table.insert(plrs,player)--Add each players into plrs table
print(player)
elseif player.IntroTag.Value == true then
for i = 1, #plrs do
if plrs[i] == player then
table.remove(plrs,i)--Remove Player from table if they are in the intro
warn(player.Name..', was Removed From Table Because They are in the Intro.')
end
end
end
end
This is bad practice to begin with. Intros are meant to be at the beginning and only played once when the player first joins. Not to mention, each player should be controlling it’s own state. You don’t want everyones state to be controlled by a singular loop on the server. That’s wasteful.
Play the intro on first game join, transition to your main UI, etc.
This code is being overcomplicated. You do not need to use table.remove, just don’t insert the player!
local Player = game.Players.LocalPlayer
local plrs = {}
for i, player in pairs (game.Players:GetPlayers()) do
if not player.IntroTag.Value then
table.insert(plrs,player)--Add each players into plrs table
print(player)
end
end
this is my new code:
Also for the if not would it work with if not player.IntroTag.Value or player.AFK.Value then ?
local plrs = {}
for i, player in pairs (game.Players:GetPlayers()) do
if player.IntroTag.Value == false or player.AFK.Value == false then
table.insert(plrs,player)--Add each players into plrs table
print(player)
elseif player.IntroTag.Value == true or player.AFK.Value == true then
warn(player, 'has Been Removed From Table - Intro Or AFK.')
for i = 1, #plrs do
if plrs[1] == Player then
table.remove(plrs,i) --Remove Player from table if they are in the intro or AFK.
Again, you are over complicating things. You do not need to remove stuff from the table, just do not insert it. You can add multiple conditions to a if statement, for example we check that the player is not afk and not in the intro:
local plrs = {}
for i, player in pairs (game.Players:GetPlayers()) do
if player.IntroTag.Value == false and player.AFK.Value == false then
table.insert(plrs,player)--Add each players into plrs table
print(player)
end
end