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
5 years ago
Edited 5 years ago
01local frame = script.Parent:WaitForChild("Frame")
02local names = {"Player1", "14dark14"}
03wait(3)
04player = game.Players.LocalPlayer
05if player.name == names then
06    script.Parent.Enabled = true
07 else
08    script.Parent:Destroy()
09    print("no")
10end

printed no even tho my name is 14dark14

5 answers

Log in to vote
0
Answered by 5 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

01local frame = script.Parent:WaitForChild("Frame")
02local names = {"Player1", "14dark14"}
03wait(3)
04player = game.Players.LocalPlayer
05for i, v in pairs(names) do
06    if player.Name == v then
07        script.Parent.Enabled = true
08        return
09    end
10end
11 
12script.Parent:Destroy()
13print("no")
Ad
Log in to vote
0
Answered by 5 years ago

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

01local frame = script.Parent:WaitForChild("Frame")
02local names = {"Player1", "14dark14"}
03wait(3)
04player = game.Players.LocalPlayer
05if player.name == names[1] or names[2] then
06    script.Parent.Enabled = true
07 else
08    script.Parent:Destroy()
09    print("no")
10end
0
Yes this would work, but it would get messy if there were many names in the table Spjureeedd 385 — 5y
0
You could use a for loop and loop through the names in the table. killerbrenden 1537 — 5y
0
I'm bad at for loops so no. User#32819 0 — 5y
Log in to vote
0
Answered by 5 years ago
Edited 5 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().

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

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
5 years ago
Edited 5 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:

01local Players = game:GetService("Players")
02 
03local player = Players.LocalPlayer
04 
05local names = {"Player1", "14dark14"}
06 
07local foundName =  false
08 
09for _, name in pairs(names) do
10    if name == player.Name then
11        foundName = true
12        break
13    end
14end
15 
16if foundName then
17    script.Parent.Enabled = true
18else
19    script.Parent:Destroy()
20end

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 — 5y
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 — 5y
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 — 5y
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 — 5y
0
I guess if it really was a problem I'll just put it in ._. xPolarium 1388 — 5y
Log in to vote
0
Answered by 5 years ago
01local frame = script.Parent:WaitForChild("Frame")
02local names = {"Player1", "14dark14"}
03wait(3)
04player = game.Players.LocalPlayer
05if names[player.Name] then
06    script.Parent.Enabled = true
07 else
08    script.Parent:Destroy()
09    print("no")
10end

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