its been 3 days and i cant figure out how to make a script detect if a player is in a table please help me fix this
local Players = game:GetService("Players") -- Get player from player services
local Player_In_Table = {
"AntivrusAVG";
"BLABLABLA";
"GAMERXDDXXDDi34yriuqwerfu";
}
for _,FindPlayer in pairs(Player_In_Table) do -- Loop
for _,Player in pairs(Players:GetPlayers()) do
if Player.Name == FindPlayer then
print("Find Player In Table")
end
end
end
@FerbZides that isn’t correct. You can’t index a string with a number.
One of these are the likely cause of the issue:
This loop only executes once, it doesn’t execute each time a player joins. So the server is empty therefore Players:GetPlayers returns an empty table and next returns nil when an empty table is provided. Therefore the nested loop breaks instantly
The casing/spelling is incorrect therefore a mismatch occurs
You are only running your code when the script starts to run, not when a new player has joined. You could use a Players.PlayerAdded event to run the code. Like so:
local Players = game:GetService("Players") -- Get player from player services
local Player_In_Table = {
"AntivrusAVG";
"BLABLABLA";
"GAMERXDDXXDDi34yriuqwerfu";
}
Players.PlayerAdded:Connect(function(newPlr)
if (table.find(Player_In_Table,newPlr.Name)) then -- player is in list
print("Found player in table")
end
end)
And by the way, GetPlayers() returns an array, use ipairs there as well. ipairs is designed to be used in arrays.
Example:
local Array = {
'abc',
'123abc'
}
local Players = game:GetService('Players')
local GetPlayers = Players:GetPlayers()
local function FindPlayerInArray()
for _, Target in ipairs(GetPlayers) do
for _, Search in ipairs(Array) do
if Target.Name:lower() == Search:lower() then
return true -- Player was found in the array
end
end
return false -- Player was not found in the array
end
end
print(FindPlayerInArray()) -- true if found, false if not
added a print that prints if a player is in a table and added game.players.playeradded table.insert and in the output it doesnt print that theres a player in the table for some reason any solutions for this??
local Array = {
}
local Players = game:GetService('Players')
local GetPlayers = Players:GetPlayers()
game.Players.PlayerAdded:Connect(function(plr)
table.insert(Array,plr)
end)
local function FindPlayerInArray()
for _, Target in ipairs(GetPlayers) do
for _, Search in ipairs(Array) do
if Target.Name:lower() == Search:lower() then
print("player in array")
return true -- Player was found in the array
end
end
return false -- Player was not found in the array
end
end
print(FindPlayerInArray()) -- true if found, false if not
for some reason my the line at the bottom doesnt name all of workspace’s children “ok”
local Array = {
}
local Players = game:GetService('Players')
local GetPlayers = Players:GetPlayers()
for i,v in pairs (workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") then
print(v.Name.." added to table")
table.insert(Array,v.Name)
end
end
local function FindPlayerInArray()
for _, Target in ipairs(GetPlayers) do
for _, Search in ipairs(Array) do
if Target.Name:lower() == Search:lower() then
print("player in array")
return true
end
end
end
return false -- Player was not found in the array
end
if FindPlayerInArray() then
for i,v in pairs (workspace:GetChildren()) do
v.Name = "ok"
end
end
Make it so if it detects a player in the table it names workspace’s children “ok”. im trying to make it so when it detects the player is does something. so i randomly thought if it detects a player in the table it name’s the workspace’s children “ok”.
The function needs to be called before it can execute. I don’t see you calling it anywhere.
And, you should move the local function above your script as the local function will be out of scope if you attempt to call it above where it actually is.
im sorry but i am very confused i called the function also my peanut brain is having trouble understanding it
local Array = {
}
local Players = game:GetService('Players')
local GetPlayers = Players:GetPlayers()
for i,v in pairs (workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") then
print(v.Name.." added to table")
table.insert(Array,v.Name)
end
end
local function FindPlayerInArray()
for _, Target in ipairs(GetPlayers) do
for _, Search in ipairs(Array) do
if Target.Name:lower() == Search:lower() then
print("player in array")
return true
end
end
end
print("didnt work")
return false -- Player was not found in the array
end
workspace.ChildAdded:Connect(function()
FindPlayerInArray()
end)
local Array = {
}
local Players = game:GetService('Players')
local GetPlayers = Players:GetPlayers()
local function FindPlayerInArray()
for _, Target in ipairs(GetPlayers) do
for _, Search in ipairs(Array) do
if Target.Name:lower() == Search:lower() then
print("player in array")
return true
end
end
end
print("didnt work")
return false -- Player was not found in the array
end
for i,v in pairs (workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") then
print(v.Name.." added to table")
table.insert(Array,v.Name)
end
end
while wait(1) do
FindPlayerInArray()
end