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

Storing names in table didn't work? help ( easy solution )

local frame = script.Parent:WaitForChild("Frame")
local names = {"Player1", "14dark14"}
wait(3)
player = game.Players.LocalPlayer
if player.name == names then
	script.Parent.Enabled = true
 else
	script.Parent:Destroy()
	print("no")
end

printed no even tho my name is 14dark14

5 answers

✓ Accepted answer

The answer by zamd157 would work. But if your table would grow in the future then it would get messy and unorganized to write it out. The best to do is to loop through your table

local frame = script.Parent:WaitForChild("Frame")
local names = {"Player1", "14dark14"}
wait(3)
player = game.Players.LocalPlayer
for i, v in pairs(names) do
	if player.Name == v then
		script.Parent.Enabled = true
		return
	end
end

script.Parent:Destroy()
print("no")

(I think) it's because you need to specify which name it is just like so:

local frame = script.Parent:WaitForChild("Frame")
local names = {"Player1", "14dark14"}
wait(3)
player = game.Players.LocalPlayer
if player.name == names[1] or names[2] then
    script.Parent.Enabled = true
 else
    script.Parent:Destroy()
    print("no")
end

3 comments

0

Yes this would work, but it would get messy if there were many names in the table

0

You could use a for loop and loop through the names in the table.

0

I'm bad at for loops so no.

Hello! 'names' is an entire table, not just a string. That's why your code didn't work, to make this work, what you have to do is get everything in the table, so using for i, v in pairs().

local frame = script.Parent:WaitForChild("Frame")
local names = {"Player1", "14dark14"}
wait(3)
player = game.Players.LocalPlayer
for i, v in pairs(names) do
if player.Name == v then
script.Parent.Enabled = true
end
end

This way its not messy and it works, accept if was helpful, otherwise tell me so

You're comparing the name of the player to a table.

Line 02 is the table and on line 05 you access the Name property which gives you a string. You need to index the table to read the value of each element.

You can do this with a for-loop to linearly search for a value in the 'names' table:

local Players = game:GetService("Players")

local player = Players.LocalPlayer

local names = {"Player1", "14dark14"}

local foundName =  false

for _, name in pairs(names) do
	if name == player.Name then
		foundName = true
		break
	end
end

if foundName then
	script.Parent.Enabled = true
else
	script.Parent:Destroy()
end

I recommend that you learn more about the basics of Lua. Specifically, loops and tables as these are concepts that will help you become a better programmer:

Let me know if you don't understand anything or if I left something out.

5 comments

0

That wouldn't work

0

The loop runs once for each name in the table, if the player's Name isn't equal to v the first time the loop runs it won't run anymore

0

Yeah nice edit, but now if the player's Name isn't equal to any name inside the table the Script won't care. You should add those lines you just removed after the loop is finished

0

Didn't catch that but thank you. I took out that else block as OP can probably use another method to disable/enable. The original problem should still be addressed though.

0

I guess if it really was a problem I'll just put it in ._.

local frame = script.Parent:WaitForChild("Frame")
local names = {"Player1", "14dark14"}
wait(3)
player = game.Players.LocalPlayer
if names[player.Name] then 
    script.Parent.Enabled = true
 else
    script.Parent:Destroy()
    print("no")
end

i thought ur destroying it then printing it but idk someone in comments tell me why but that's not how u check through tables

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