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

How do I check to see if something is in a table for an if statement?

I'm so awful with tables it's not even funny. Well, it might be to someone out there, actually.

Here's basically what the important bits of my code look like. P is defined properly in the actual code, but for the sake of brevity is not here.

admins = {"chrono2","player1"}

p = game.Workspace.Player --blagh that's ugly

if p.Name = admins then
print(p.Name .. " is an admin!")
end
else 
print(p.Name .. " is not an admin!")
end

The script will run properly, but then fail to execute past the if statement. I guess because the name of the player doesn't equal the entire array? I've dived through the wiki, and I still have no idea what I'm doing wrong here.

1 answer

✓ Accepted answer

There is only one way you could go about solving this problem, which is a for loop

This will simply go through the table, and check if each variable is equivalent to whatever you want it to:

for i=1, #admins do
	if admins[i] == p.Name then
		--Do stuff, this will work
	end
end

Contrary to popular belief, just looking for the player name in an if statement will not work

if admins[p.Name] then
	--This will not work
end

So in conclusion, there is no was you could use just an if statement, you would need to add a for loop and place the if statement inside of it

Here is your final script:

admins = {"chrono2","player1"}

p = game.Workspace.Player --blagh that's ugly

for i=1, #admins do
	if p.Name == admins then
		print(p.Name .. " is an admin!")
	else 
		print(p.Name .. " is not an admin!")
	end	
end

I hope this helped!

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