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

String Inside Module not working?

Asked by 4 years ago

Hello there! I have a ModuleScript with this simple code:

local string = {"MyString1","MyString2"}

then I have a normal script that checks a textbox for the text:

local module = script.ModuleScript
local string = module.string
if script.Parent.text == string then
print("YOU GUESSED RIGHT!")
0
You need to `return string` at your module, and require it in another scirpt User#24403 69 — 4y
0
I've typed return string and tried the require but not to sure what to do, are you able to fix the script? JamiethegreatQ777 16 — 4y

1 answer

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

string is a table, not a string, overrides the built-in table by the same name, and isn't part of the module. You've declared it in the scope of the script rather than part of the module in this case, and as Incapaxx has said, you also need to require it in order to load it.

Firstly, in order to load it up, use the reference to the module as the sole argument in the require function and save its return value, which would look like this:

local module = require(script.ModuleScript)

Now the module will actually be loaded. Next, you'll want to make the string part of the module instead of local to the script, compare the guess to a value inside the table, and change its name so you're not overriding the built-in string table.

I'm not sure why you have multiple strings has the correct answer, but I'll assume it's because you want multiple correct answers. In this case, the table should be turned into a dictionary, with the correct answers being stored as keys, and the values being true.

module.correctStrings = {MyString1 = true, MyString2 = true}

Now, instead of comparing the guessed string to the correct string, you'll want to use the guessed string as a key to index the correctStrings table and see if it indexes true.

local correctStrings = module.correctStrings
if correctStrings[script.Parent.Text] then
    --A correct string has been guessed.
end
Ad

Answer this question