The function in Module Script does not work

:wave: Hello

When I try to call a function in a script module, I get an error in the output:

attempt to call a nil value

I don’t know why it appears, so I’m creating this topic.

Code:

  • Script:
print(chosenSword.Id)
SwordsService.GetSwordBasicDamage(chosenSword.Id)
  • Module Script
function GetSwordBasicDamage(Id)
	GetSwordBasicDamage = Swords[Id].swordDamage
end

Images:

Error:
Error

Script:
Script

Module Script:
Module Script:

Shouldn’t it be this instead?

function SwordsService.GetSwordBasicDamage(Id)
end
1 Like

First of all you need to do require(SwordService) (if you didnt already)

and also can you send a screenshot of the whole script? there is some important stuff i need to know in the script

when using a ModuleScript, you have to define where the function will be, so if you want it to be part of the table, you say table.modulefunction instead of just modulefunction, because when the you are requiring the ModuleScript, it is returning the table that was inside the ModuleScript, hence the return module at the bottom of every ModuleScript, if its not defined in this table, it wont be sent to the Script.

There are two operators you can use. A Period . and a Colon :

Despite them doing the exact same thing to add the function to the table, they have completely different purposes, and involve what is known as OOP, Object Oriented Programming.

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