Answered by
5 years ago Edited 5 years ago
Question
How to access a dictionary in ModuleScripts?
Answer
The same way you index a dictionary anywhere else.
Whats wrong with your code?
It appears you're trying to index the string "blabla"
for player.Name
which will not work. Try setting Blacklist
to a dictionary its self, and then put the names of the players as the keys for the dictionary, and the value to true
or any value with truthiness.
Example:
03 | local configuration = { |
05 | [ "cowboyemote" ] = true |
13 | local module = require(<module reference>) |
15 | local player = <some player> *(assume its me in this case)* |
17 | if module.blacklist [ player.Name ] then |
18 | print (player.Name, "is blacklisted!" ) |
20 | print (player.Name, "is not blacklisted!" ) |
Should have an output of (assuming I'm the player you're checking):
cowboyemote is blacklisted!
Because my name is a key in the blacklist
table, and has a truthy
value set to the index, the if then
statement will continue as true.
What is truthiness?
Truthiness is a property of every datatype. Think of it as a value which identifies whether the object in question really exists.
Somewhat vague answer ):
If you have a variable set to a string "hello"
and use an if statement on that variable you will notice the if statement continues. This is because the string is there, it exists.
This means things like nil
and false
do not have truthiness.
Reading Resources
http://lua-users.org/wiki/TablesTutorial
[https://www.lua.org/manual/2.4/node3.html)(https://www.lua.org/manual/2.4/node3.html)