ModuleScript functions acting weird

I found an answer to my problem, but is there a reason why every first argument of every function inside a ModuleScript returns as the module table itself?

I do not understand the question? Are you talking about method calls? (a:b(...) for example?)

I mean, if I do

local module = {}

function module.someFunction(a)
    print(a,module)
end

and then do

local module = require(script.ModuleScript)

module:someFunction()

it’ll print the same two things

(Sorry if this is hard to understand it’s my first post)

a:b() is syntactic sugar (a nicer way to write something) for a.b(a). So this is not weird behaviour at all.

except every other time I did function module.someFunction(a) a wasn’t equal to module

I do not understand the question.

function a:b()

end

Is sugar for

function a.b(self)

end

which itself is sugar for

a.b = function(self)

end
1 Like

what do you mean? It doesn’t even have to be the module.

function module:a() 

equates to

module.a = function(self)

I’m aware, but I’m trying to do

function module.someFunction(arg1)

and having arg1 not be the module

I recreated what you did exactly to see what you mean. The output for me when running your function is table: 0x7e66a79df293a88b table: 0x7e66a79df293a88b. I do not know why this is happening. For some reason the first argument is returning the module.

I will try to explain how to re-create this. Make a new module, make a function inside,

function module.someFunction(a)
    print(a,module)
end

Require the module and run the function. For some reason, this will output the module itself for both prints.

EDIT: I originally ran it in the command bar. The same thing happened in a script. Also, I tried using game.Workspace for the first argument. It still was printing the same thing. I have 0 clue why this happens though.

yes, I did that(not exactly but in general) and a was the module and b was 3 in this example

Ok, I understand. I completely did not realize there it was using module.function() and then calling it with module:function()

local module = {} 

    function module:add(a,b) 
    return a+b end
    
return module

from the command bar :

module  = require(game.ServerScriptService.ModuleScript) 
a = module:add(3,4) 
print(a) ---> 7

It’s not. you think I’m doing

function module:someFunc()

while I’m doing

function module.someFunc()

Is there a specific reason you are calling it with a colon when its signature uses a period? You generally should go with the operator the function signature uses

I AM using a period.I never knew

function module:func()

existed before

…I was asking why you were using a : to call it when you defined the function with a .

a:b() and a.b() are not the same.

1 Like

Oh there’s a difference? I always thought you needed to use : to call a function

Then what’s the issue?
It works.

This is not weird behavior at all, when you do

	local module = {}

	function module.someFunction(a)
    	print(a)
   end

    return module 

And then use that function,

local module = require(game.ServerScriptService.mod:Clone())
-- ignore the clone, I did it to circumvent the module not updating each time I change it
module["someFunction"](2) --> prints(2), passed as param.a


What is the need to attempt to print a module?
You are essentially attempting to do this :

print({})
---> prints a memory address, similar to doing

local module  = {}
print(module) ---> prints a memory address for an empty table

I don’t need to print a module, I was trying to print something else, but the module was printing instead. Anyways it’s solved