Making a simplistic gun system for a fan recreation of a game called “Noobs vs Zombies: Relish Reborn”. Anyway, I’m trying to access the self variable from the first function from above. The issue is that I don’t know how to.
--[[MODULE]]--
local GunSystemModule = {}
GunSystemModule._index = GunSystemModule
function GunSystemModule.Equip(Weapon)
local self = setmetatable({}, GunSystemModule)
--//Gun Variables
self.Weapon = Weapon
self.Animations_Folder = Weapon:FindFirstChild("AnimationFolder")
self.TestVariable = 3
--//Equipping
self.Weapon.Equipped:Connect(function()
print(self.Weapon.Name)
end)
return self
end
function GunSystemModule.Raycast(self, MousePos)
--//TBA
end
return GunSystemModule
You should be able to just switch to semicolons which IIRC automatically pass the self variable to the method argument which would mean you don’t need to explicitly define it and can just access it
Sorry for the late response, but I’ve tried doing that and for some reason, whenever I try printing just self on the raycast function, it’s apparently printing out a table of both the functions alongside the .__index part.
--[[SERVICES]]--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--[[MODULE]]--
local GunSystemModule = {}
GunSystemModule.__index = GunSystemModule
function GunSystemModule:Equip(Weapon)
local self = setmetatable({}, GunSystemModule)
--//Gun Variables
self.Weapon = Weapon
self.Animations_Folder = Weapon:FindFirstChild("AnimationFolder")
--//Equipping
self.Weapon.Equipped:Connect(function()
print(self.Weapon.Name)
end)
return self
end
function GunSystemModule:Raycast()
print(self)
end
return GunSystemModule
That’s what you would expect though, isn’t it?
When dealing with metatables, the outputs of print() get a bit messy, so you would be better off just printing off the information you want (e.g. specific properties, such as .Weapon)