I’m trying to make a dash system and this is my second time using the OOP method, but for some reason when i try to print the value of the OOP parameter, it just returns a nil value.
This is my Local Script:
local hrp = game:GetService("Players").LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local TypeOfVelocity = Instance.new("LinearVelocity")
TypeOfVelocity.Parent = hrp
TypeOfVelocity.Attachment0 = hrp.RootAttachment
TypeOfVelocity.ForceLimitMode = "PerAxis"
local Dashing_Module = require(game.ReplicatedStorage.Scripts.Movement_System:WaitForChild("Dashing_Module"))
local Dashing = Dashing_Module.new(TypeOfVelocity)
This is my module script:
local Dashing = {}
Dashing.__index = Dashing
function Dashing.new(Type:LinearVelocity)
local New_Dash = {}
setmetatable(New_Dash,Dashing)
New_Dash.Type = Type
return New_Dash
end
function Dashing:Dash()
print(self.Type)
end
return Dashing
If you have any other tip or post to help me improve my OOP code feel free to comment
Genuine question, did you just forget to call Dashing:Dash() from your LocalScript?
This code runs fine and your Dash object is created as it should be.
Is something happening that I’m not understanding? If that’s the case then you may need to re-explain.
In Lua(u), Table:Function(...) is actually syntax sugar for Table.Function(Table, ...). The property “Type” is stored within the object, which is the table produced by your class’ constructor. The object itself contains only properties, yet you’re still able to invoke non-existent functions from the object. This is because, upon attempting to invoke them, the __index metamethod redirects Lua(u) to the class where those functions are defined. This leads to the following translation:
Object:Method(...)
-- Translates to:
Class.Method(Object, ...)
self assumes a reference to Object, which is how you access the properties of the object the method of your class is called on. When you write Class:Method(...), however, that translates to:
Class.Method(Class, ...)
self is no longer referring to the object, but the Class table. Your Class table has no “Type” property, which is why you observe nil