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

[SOLVED] How to access a dictionary in ModuleScripts?

Asked by
Miniller 562 Moderation Voter
5 years ago
Edited 5 years ago

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!

1 answer

Log in to vote
1
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.

dictionary[key]

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:

--// 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.

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)

0
Thank you! Miniller 562 — 5y
Ad

Answer this question