Table detection script not printing that a player is found

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

That’s the code above try it and let’s see you still need some use for the index in order for it to work

@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

Thats how I did my admin only guis tho

I found that method on a free model

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

Not exactly like that. See:

image

You are indexing the string with a number.

Either way you should use table.find. Linear searches are now obsolete and are reinventions of the wheel.

it loops through every string inside that table and it is inside ipairs

The index is not only a number but the Strings that are being looped through l

A better solution would be this:

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
1 Like

That wouldn’t be good.

If a player named Hello123 is in the array,
and a player named Hello123123 joins, you will run into an issue.

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

You can do

if FindPlayerInArray() then
   -- code
end

It doesn’t print because it’s not returned, if you wanted something like that, you could do:

return 'Player found'
print(FindPlayerInArray())
1 Like

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

What are you trying to do exactly?

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.

You should try experimenting yourself.

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)

output: didnt work

like this?

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