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

Question about tables?

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)

2 comments

0

Because to the script, Player1 is the index value. Which you do end up searching for when you index banlist on line 5.

0

Does it work for everything or just usernames? Could it work for part names too if it wasn't a ban script?

2 answers

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

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"`

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