I'm trying to make a votekick script and I use a modulescript for it, however I can't get it to work. Let's say I have a module script what looks like this:
local Configuration = { ["Blacklist"]="blabla" } return Configuration;
My script detects if the user writes ;votekick to the chat. If a user is blacklisted, he can't votekick, that's why I need to detect it. So in the script:
local module = require(script.Parent) -- The script is a child of the modulescript --and then under the message detect thing: if module.Blacklist[player.Name] == nil then print("yay got 1 vote") else print("BLACKLISTEd") end
There are no errors, however it always prints yay got 1 vote
. I never used modulescript before so I don't know how to do this. Thanks!
How to access a dictionary in ModuleScripts?
The same way you index a dictionary anywhere else.
dictionary[key]
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.
--// Module local configuration = { blacklist = { ["cowboyemote"] = true } } return configuration --// Script local module = require(<module reference>) local player = <some player> *(assume its me in this case)* if module.blacklist[player.Name] then print(player.Name, "is blacklisted!") else print(player.Name, "is not blacklisted!") end
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.
Truthiness is a property of every datatype. Think of it as a value which identifies whether the object in question really exists.
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.
http://lua-users.org/wiki/TablesTutorial [https://www.lua.org/manual/2.4/node3.html)(https://www.lua.org/manual/2.4/node3.html)