Attempt to index number with any module function

I made a timer module that converts to M:S and M:S.MS. But somehow it errors when I typed in the command bar to convert the timer seconds

Here is the code of the timer module: game.ReplicatedStorage.TIMERLIB

local MOD = {}

function MOD.M_S(SECONDS : number)
	local ABSOLUTESECONDS = math.abs(math.floor(SECONDS))
	
	local MIN = math.floor(ABSOLUTESECONDS/60)
	local SEC_TENS = math.floor(ABSOLUTESECONDS/10%6)
	local SEC_ONES = ABSOLUTESECONDS%10
	
	local TIMERTEXT = SECONDS >= 0 and MIN..':'..SEC_TENS..SEC_ONES or '-'..MIN..':'..SEC_TENS..SEC_ONES
	
	return TIMERTEXT
end

function MOD.M_S_MS(SECONDS : number)
	local ABSOLUTESECONDS = math.abs(SECONDS)
 
	local MIN = math.floor(ABSOLUTESECONDS/60)
	local SEC_TENS = math.floor(ABSOLUTESECONDS/10%6)
	local SEC_ONES = math.floor(ABSOLUTESECONDS%10)
	
	local MIS = ABSOLUTESECONDS%1*1000
	
	local MILLISECONDSTEXT = tostring(MIS):sub(1,3)

	local TIMERTEXT = SECONDS >= 0 and MIN..':'..SEC_TENS..SEC_ONES..'.'..MILLISECONDSTEXT or '-'..MIN..':'..SEC_TENS..SEC_ONES..'.'..MILLISECONDSTEXT

	return TIMERTEXT
end

return MOD

And here is what I typed in the command bar:

local Module = require(game.ReplicatedStorage.TIMERLIB) 
print(Module.M_S(17.655))

Guys, I don’t know what to do?

1 Like

You’re implying that there’s an error message, can I see it?
Also I never really used the command bar before, but perhaps you can’t store variables in it. Have you tried print(require(game.ReplicatedStorage.TIMERLIB).M_S(17.655))?

image

1 Like

Yes, I tried that… it somehow errors too

1 Like

Ok so what I’m getting from this is that whatever require(game.ReplicatedStorage.TIMERLIB) returns is a number. Maybe try renaming the table in the module script from MOD to something else? The name might be clashing with a math constant or something.
Also, make sure that the return at the end of the module script actually returns the table and not a number.

Change the function methods from . to :
you’re supposed to name them this way
function MOD:M_S(SECONDS: number)
and
function MOD:M_S_MS(SECONDS : number)
and use them this way print(Module:M_S(17.655)

for the method MOD.M_S() the first parameter I reckon is self and that is why its saying attempt to index nil for seconds parameter, I haven’t gone through this breifly yet and I am rusty with Object Oriented Programming but that should fix it.

Assigning a function with . doesn’t pass self in automatically. You can still pass it in manually (which I do for type autocomplete):
function someModule.someFunction(self : someModuleType, ...)

Pretty much function someModule:someFunction(...) is the same as function someModule.someFunction(self, ...)

1 Like

Oh, it turns out the previous reloaded module is ‘return [number]’ but the module won’t update the code when I set it to ‘return MOD’. So that’s why I got the error.
The only way I fix it is make the ModuleScript error, reload the ModuleScript, remove the
ModuleScript error, and reload the ModuleScript. And it finally worked again. Thank you guys so much.
image

1 Like

My bad, I wasn’t paying attention enough, thanks for the correction.

1 Like

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