Is it possible to scan all the options in a module?

Is it possible for a server script to scan all the options in a module script? What I mean is instead of having like 30 if statements, Could you just get all the options in a module and look for the value im looking for?

2 Likes

I’m not entirely certain what you mean by options in a module script.

Maybe you’re looking for a dictionary?

local module = {
	exaple = "Example";
	exaple2 = "Example2";
}

return module

The options are example1 - 2, Sorry for the confusion, I want to know how a script could look though all the “options” and if they find them they return true

1 Like

You could do just module[lookingFor] which is a truthy value if lookingFor exists… well, unless the value is false, but you can do a ~= nil if you only want to know it exists.

Say the value is a string, How could you check that? Please can you send a quick sample script thx

1 Like
if(module[“potato”]) then
    print(“potato was found and not false”)
end

if(module[“potato”]~=nil) then
    print(“potato exists”)
end
1 Like

Oh, That isn’t what I meant, Ill re-explain myself, Sorry:

Basically, I have a localscript that sends data to the server (from a textbox) And if that text from the textbox matches with one of the strings in the module, Then it allows access, I dont want to do an if statement for every single key, My question was how you can scan through all the different keys in a module to see if one matches, Sorry for the confusion.

1 Like

My understanding of your question is that you want to check if a value is in a dictionary, so the easiest way would be to use a for loop.

local function scanTable(tab)
	for k, v in pairs(tab) do
		if v == optionToFind then
			return true
		end
	end
	
	return false
end

An alternative is to structure your ModuleScript like this, with the options being the keys:

local module = {
	["Option1"] = true,
	["Option2"] = true,
}

This way, you don’t have to use a for loop and you can just check module[option], which will return true if the option exists.

1 Like

Can you make the string you are searching for the key of the dictionary? That’s generally how I try to structure these. That way you can just do like module[text] = true to set it and module[text] = nil to unset it. Then you can just do module[text] to check if it exists. This will only work if the keys you already make don’t hold to much significance though.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.