How would I get the Self variable from the first function of the module script?

Heya there.

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

1 Like

Try this, I don’t use OOP myself, but I think this is what your looking for.

--[[MODULE]]--
local GunSystemModule = {}
GunSystemModule.__index = GunSystemModule

function GunSystemModule.Equip(Weapon)
	local self = GunSystemModule.__index

	--//Gun Variables
	self.Weapon = Weapon
	self.TestVariable = 3

	--//Equipping
	
	--[[
	self.Weapon.Equipped:Connect(function()
		print(self.Weapon.Name)
	end)
	--]]
	
end

function GunSystemModule:Raycast(MousePos)
	
	print(self.TestVariable)
	
	--//TBA
end

return GunSystemModule

1 Like

This accepts self as the first parameter, meaning you must pass it in manually.

function GunSystemModule.Raycast(self, MousePos)
	--//TBA
end
--to use
MyGun.Raycast(MyGun, myMousePos)

This automatically passes self, allowing you to use the keyword ‘self’ without defining it.

function GunSystemModule:Raycast(MousePos)
	--//TBA
end
--to use
MyGun:Raycast(myMousePos)

Either should work.

1 Like

Self should already be defined if its a method, you shouldn’t have a problem doing it like this:

function GunSystemModule:Raycast(MousePos)
	--//TBA

--Use self all you want now.
end

its __index not _index btw.

GunSystemModule.Equip(GunSystemModule, pos)

is same as

GunSystemModule:Equip(pos)

: will automatically pass self which is GunSystemModule.

same in this situation

function GunSystemModule.Equip(self, pos)

is same as

function GunSystemModule:Equip(pos)

: will automatically declare self as parameter which is GunSystemModule

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)

I suppose you’re correct, but how would I try getting said properties?

e.g. self.Weapon

Yeah I’ve actually managed to figure it out by myself, thanks for all of your help.

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