Help with module scripts

I made a script where it accepts a certain users inside the module scripts, otherwise, it deletes a part.
I tried many of the resources but they aren’t working as needed.
This is my script:

SCRIPT:

local UserModule = require(game.ServerScriptService.ModuleScript)

if game.CreatorId == table.find(UserModule, game.CreatorId) then
	game.Workspace.Part.Anchored = true
else
	game.Workspace.Part:Destroy()
end

Module Script

local module = {
	
	 1783349062
	
}

return module

Please tell me if there is any problem with it and how I can fix this issue.

you need to call the module with a function, you cant blank fire a module like that

module = {
    module.GetIds()
        return {1783349062} -- Add the other players
    end
}
return module
-------------------------------
local UserModule = require(game.ServerScriptService.ModuleScript)

if game.CreatorId == table.find(UserModule.GetIds(), game.CreatorId) then
	game.Workspace.Part.Anchored = true
else
	game.Workspace.Part:Destroy()
end

A module itself is an array of functions, so when you require a module you are prompting the module script to return all of the functions it has. I’m surprised you didn’t get an error message just putting the numbers in there to be honest

This should work fine:


SCRIPT:

local UserModule = require(game.ServerScriptService.ModuleScript)

if table.find(UserModule, game.CreatorId) then
	game.Workspace.Part.Anchored = true
else
	game.Workspace.Part:Destroy()
end

Module Script

return {
	 1783349062
}

technically you could do this in a module script

return 0

the actual reason why this doesn’t work is because table.find returns the table index/key!
obviously the CreatorId won’t equal a small index of 1

A module can contain a table of numbers and that on its own should be fine. You can also add functions to ModuleScripts. They can keep your code organized by containing functions that you often use. As long as the ModuleScript has return values and you’re requiring them properly, you shouldn’t have a problem. Try it out yourself on Roblox Studio.

Create a ModuleScript in ServerScriptService and put this inside.

return {1,2,3,4,5}

Create a Script and put this inside.

local UserModule = require(game.ServerScriptService.ModuleScript)

print(UserModule) -- You will see that the table prints out.
print(UserModule[1]) -- You will see that the first index in the table prints out.
1 Like

Not necessary, a module does not have to contain a function. When require is called on it, if it is the first time, it runs the script and stores the returned result. If it is any subsequent time, it returns the same result. That allows you to have shared tables between scripts.

I agree with @Ailore and @CreditsMix
honestly I feel like the answer @R0bl0x10501050 gave is good enough

2 Likes