Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you compare a string to a table?

Hiya, I currently have a table set up with a list of game admins in it called "Admins". I'm trying to figure out if its possible to check if the players name is the same as any of the strings in the Admins table. (I already have everything else scripted I just don't know how to compare a single string to a table). Thanks

local Admins = {"AdminName1","AdminName2",}
local player = game.Players.LocalPlayer

TL;DR I'm trying to check if the player.Name == one of the names from the Admins table.

2 comments

0
0

Thanks I don't know how I didn't think of using a loop

2 answers

for i,v in pairs(Table) do
    if v == game.Players.LocalPlayer.Name then
        print("hello"..plr.Name.."!")
    end
end

A very basic way is to use a for loop:

local Admins = {"AdminName1", "AdminName2"}
local player = game.Players.LocalPlayer
-- Loop through the admins
for _,admin_name in pairs(Admins) do
    -- Compare admin name to player name
    if admin_name == player.Name then
	    print("True")
	    -- Do Stuff
	end
end

Another method is to use a dictionary

local Admins = {
    ["AdminName1"] = true,
    ["AdminName2"] = true
    }
if Admins[player.Name] then
    print("True")
    -- Do Stuff
end

Since the player name is a key, it can access the element, which is true. Therefor allowing you to run whatevers inside the if statement block.

This discussion is preserved as it was. New answers, comments, and votes are closed.