Hello! I am calling a function like below but the problem is, is that self is not getting defined.
Sword handler is a module script that needs self, but when I call this function self returns nil.
I may be dumb but correct me if I am wrong!
Hello! I am calling a function like below but the problem is, is that self is not getting defined.
I may be dumb but correct me if I am wrong!
Could you show us what SwordHandler looks like?
Also, you’d use self with methods so you wouldn’t have to manually pass a value as self.
Item:Method() -- Item is self
Here is the sword handler code (Note that what I gave is not the whole code for the sword handler)
You are using a method in the example you showed me. Colon notion dictates a method and self is redundant in that instance. Methods don’t make the code any longer compared to normal functions either, it reduces the amount of values you need to plug in as parameters into a function.
To me it looks like self in this instance isn’t referring to Main which you called the method on. I would personally forgo using self and instead call it Sword or something.
Ex:
function Main:Slash(Sword, Damage)
-- Replace self as Sword or whatever else you want it to be. Self in this method is referring to Main because you called the slash method on Main. Using self again is just going to be confusing.
end
I did that before creating a forum post but made “Sword” nil
I planned to make it so that I wouldn’t have to call each move/ability each time I would make a new one or simply press a key to check which function to use the move.
Correct me if I am wrong
Try changing the colon in Main:Slash to a dot and see if that helps.
As for what I’m replying to I’m not quite sure what you mean. Are you trying to make a database that you can call functions from based on a key or a name?
All I had to do was change the semicolon to a dot😭 Thanks for the support!
No problem. I’ve had similar issues with notations so I’m glad this helped. When in doubt, change the notation!
For further clarification, the issue here is that colon notation makes self
the implicit first parameter. Hence, you don’t need an explicit self
parameter in your method signature:
function Main:Slash(Damage)