Using Module Scripts

Hello,
I just came across something while I was coding and I do not know how to implement it. So, I have a Module Script and what I want it to do is give permission to any userId mentioned in the Module Script.

Module Script

local module = {
	
	User1 = 1792448898;
	User2 = 1783349062
}

return module

Script

local Users = require(game.ServerScriptService.ModuleScript)

if game.CreatorId == Users.User1 or Users.User2 then

print("Success!")

else

print("Failure")

end

Instead of having to type ‘or’ always, I want the script to take in any UserId in module script and accept it. Is it possible to do something like that?

if game.CreatorId == Users.User1 or game.CreatorId == Users.User2 then

fixed that if statement

1 Like

You can use table.find() to see if a given array has a specified value somewhere in the array.

And due to how conversions to Boolean values work, if the value is listed in the table it will return true.

2 Likes

use a for loop

local Users = require(game.ServerScriptService.ModuleScript)

for User, Id in pairs(Users) do
    if game.CreatorId == Id then
        print("Success!")
    else
        print("Failure")
    end
end

EDIT: fixed the code sorry

That won’t work as intended, it would just print Failure for #Users-1 times.

1 Like

I edited it before you replied to me

That still is just gonna print Failure every time the code is ran, even if the Creator’s ID is listed.

1 Like

I was talking about not putting in “(Users)” after pairs, that would error…

I have my user ID in the module script but it still prints “Failure” x2 times.

@D0RYU’s code is telling you if each value within the array is the creator’s ID or not. I would just recommend using table.find() as it’s a built in library to handle finding if a certain value exists in an array.

yea sorry, I must have read the problem wrong, my bad

It would be much better if instead of this:

you do this:

local module = { 
    creators = { 
         1792448898, -- User 1
         1783349062  -- User 2
    }
}

return module

This way, if you need to add or remove an accepted user, you can without needed to modify multiple files. To implement this for your code (i.e. Script), I would make a function called MadeByTeam() (or whatever you think makes sense) that will return a String (either "Success!" or "Failure").

local AcceptedCreators = require(game.ServerScriptService.ModuleScript)

function MadeByTeam()
  
    -- Handles the case when the AcceptedCreators.creators list is empty
    -- as otherwise the code would fail in that situation.
    if (#AcceptedCreators.creators == 0) then
        return "Failure"
    end

    for i = 1, #AcceptedCreators.creators do
        if game.CreatorId == AcceptedCreators.creators[i] then
            return "Success!"
        end
    end

    return "Failure"
end

-- prints out either "Success!" or "Failure"
print(MadeByTeam())

The for loop checks if the game’s creator is any of the accepted creators, and returns “Success!” if that is the case. If the loop has went through all of the accepted creators, and it hasn’t returned anything yet, it means that the creator is not an accepted creator and thus the program will return “Failure”.

1 Like