Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by
14dark14 167
4 years ago
Edited 4 years ago
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

Log in to vote
0
Answered by 4 years ago

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")
Ad
Log in to vote
0
Answered by 4 years ago

(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
0
Yes this would work, but it would get messy if there were many names in the table Spjureeedd 385 — 4y
0
You could use a for loop and loop through the names in the table. killerbrenden 1537 — 4y
0
I'm bad at for loops so no. User#32819 0 — 4y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
xPolarium 1388 Moderation Voter
4 years ago
Edited 4 years ago

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.

0
That wouldn't work Spjureeedd 385 — 4y
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 Spjureeedd 385 — 4y
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 Spjureeedd 385 — 4y
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. xPolarium 1388 — 4y
0
I guess if it really was a problem I'll just put it in ._. xPolarium 1388 — 4y
Log in to vote
0
Answered by 4 years ago
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

Answer this question