Why does my ModuleScript not work?

local magic = {}

function magic:Loader()

for i,v in pairs(game.Players:GetPlayers()) do

script.GUI:Clone().Parent=game:GetService('Players')[v.Name].PlayerGui

end

end

return magic

I am making a script so that, when executed in console > server, will work. It is supposed to add a maintenance screen onto the screen, but for some reason, upon executing, I get:
16:02:43.899 - require(ID):Loader():1: attempt to index function with ‘Loader’

My require I fire into console > server is:

require(ID):Loader()

: is used as synctatic sugar, i.e
a:b = a.b(a)

change : to . (Dot)

as in

function magic.Loader()

for _,player in ipairs(game:GetService("Players"):GetPlayers()) do
    
  -- etc.

Also try to set the parent instance after cloning the Gui, like

local clone = script.Gui:Clone()
        clone.Parent = player:WaitForChild("PlayerGui")

You would be doing :

function magic.Loader()

local magic = {}

for _,player in ipairs(game:GetService("Players"):GetPlayers()) do
        local clone = script.Gui:Clone()
        clone.Parent = player:WaitForChild("PlayerGui")
    end

return magic

Requiring private modules by ids doesn’t work anymore, require the Module instance.

Like this?

local magic = {}

function magic.Loader()
for _,player in ipairs(game:GetService("Players"):GetPlayers()) do
        local clone = script.Gui:Clone()
        clone.Parent = player:WaitForChild("PlayerGui")
    end

return magic
1 Like

You’re requiring the wrong module apparently, because the one you do returns a function instead of a table.

1 Like

The reason why this error happens is because self gets defined as require here, so it’s attempting to call Loader on require which isn’t right. This module would’ve worked by assigning the returned table from the module to a variable, then calling it from there.

local module = require(ID)
module:Loader()

I’m pretty sure this is the case, at least.

If you aren’t using self, then you should try not to use colon. I know it looks fancy and all but hardly anyone’s going to see that. Judging by the way your code looks too, don’t think you’d want to be doing that anyway. I strongly advise settling for the dot.

2 Likes

No, that’s not how it is evaluated. First the require function loads the module and returns whatever the module has returned, then we try to index the returned value/reference with Loader and if it is possible and the Loader field exists then it is called. So these two do the same thing:

local module = require(ID)
module:Loader()
require(ID):Loader()

The only possible cause of a problem in this situation is that the OP is requiring the wrong module which returns a function instead of a table containing Loader method.