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

Question about tables?

Asked by 8 years ago
Edited 8 years ago
banlist = {Player1 = true} -- why does this work, isn't it supposed to be a string?

game.Players.PlayerAdded:connect(function(player)
    repeat wait() until player.Character
    if banlist[player.Name] then
        player:Kick("You have been banned from this game due to hacking activities :(")
    end
end)
0
Because to the script, Player1 is the index value. Which you do end up searching for when you index banlist on line 5. M39a9am3R 3210 — 8y
0
Does it work for everything or just usernames? Could it work for part names too if it wasn't a ban script? Heavening 10 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

What you are doing is dictionaries. In a dictionary table, there is a key and a value. The key indexes the dictionary to get the value, example:

local PlayerInfo = {
    Name = "TheLowOne",
    Age = 99,
    Job = "Sitting at home"
}--Dictionary

print(PlayerInfo.Name) -->TheLowOne
print(PlayerInfo.Age) -->99
print(PlayerInfo.Job) -->Job

Another way of constructing the table is

local PlayerInfo = {
    ["Name"] = "TheLowOne",
    ["Age"]  = 99,
    ["Job"] = "Sitting  at home"
}

Dictionaries are not limited to only stringed keys, it can be any non nil value

local Switch = {
    [true]=false,
    [false]=true
}--in this example, booleans are the key, and booleans are value 

print(Switch[true]) -->false
print(Switch[false]) -->true

More information at http://wiki.roblox.com/index.php?title=Table#Dictionaries

Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago
Edited 8 years ago

Lua provides a convenient sugar for tables. Since you often want to make properties, and it's nice to use .property instead of [somevalue], Lua automatically converts .property into ["property"]:

local obj = {}
obj.foo = 10
print(obj["foo"]) --> 10 using `.` is the same as indexing by a string
obj["foo"] = 15
print(obj.foo) --> 15

They provide a similar sugar for table literals:

local obj = {
    foo = 5,
    ["bar"] = 10,
}

print(obj["foo"]) --> 5
print(obj.bar) --> 10

Careful

Be aware that using [] without the quotes is a very different operation:

local property = "cat"

local obj = {
    [property] = 5
}
print(obj.property) --> nil
print(obj["cat"]) --> 5 -- this is where it was stored!
print(obj.cat) --> 5 -- further underscoring that `obj["cat"]` is the same as `obj.cat`
print(obj[property]) --> 5 -- since `property` as a variable has the value `"cat"`

Answer this question