How would I be able to access self variables from a base class to a subclass?

Heya Everyone!!

I’m working on a framework that tries to make NPC’s mimic as real players. The issue is that I’m trying to access self.Robotxian from ROBOTXIAN_MAIN (The base class) within a subclass (BUILDER_CLASS). How would I go accessing it?

ROBOTXIAN_MAIN

--[[INFO]]--
--//ROBOTXIAN_MAIN is the base class of both the 'Builder' and 'Fighter' class.
--//This contains essential functions for both class to work such as pathfinding or even creating Robotxians in the first place.

--[[SERVICES]]--
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")

--[[FOLDERS]]--
local Events_Folder = ReplicatedStorage:WaitForChild("EVENTS")
local Preset_Player_Models_Folder = ServerStorage:WaitForChild("PRESET_PLAYER_MODELS")

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

--//Spawning and despawning the robotxian.
function ROBOTXIAN_MAIN_FUNCTION.CreateNewRobotxian()
	local self = {}
	
	--//Just to be safe, I'd assume  this is where I'd spawn them in.
	self.Robotxian = ServerStorage.PRESET_PLAYER_MODELS.R6:Clone()
	self.Robotxian.Parent = game.Workspace
	
	return setmetatable({}, ROBOTXIAN_MAIN_FUNCTION)
end


--//Pathfinding
function ROBOTXIAN_MAIN_FUNCTION:Pathfind()
	--//TBA
end

return ROBOTXIAN_MAIN_FUNCTION

BUILDER_CLASS

--[[INFO]]--
--//The 'Builder' class is one of the two classes a robotxian can have.
--//As the name says, robotxians with the class will be able to spawn in parts and modify them to their liking.
--//Additionally, robotxians will also be able to apply both decals and a handful of special effects such as Fire or Sparkles.
--//Generally, robotxians with the class will just spawn in random parts but will occasionally start building actual stuff such as a small house or a poorly made robloxian.

--[[BASE CLASS]]--
local ROBOTXIAN_MAIN = require(script.Parent.Parent.ROBOTXIAN_MAIN)

--[[MODULE]]--
local BUILDER_CLASS_MODULE = setmetatable({}, {__index = ROBOTXIAN_MAIN})
BUILDER_CLASS_MODULE.__index = BUILDER_CLASS_MODULE

function BUILDER_CLASS_MODULE.CreateNewBuilderRobotxian()
	local self = setmetatable(ROBOTXIAN_MAIN.CreateNewRobotxian(), BUILDER_CLASS_MODULE)
	
	return self
end

function BUILDER_CLASS_MODULE:Build()
	while task.wait(5) do
		local Part = Instance.new("Part", game.Workspace)
	end
end

return BUILDER_CLASS_MODULE

since you’re never actually returning self and not referencing it anywhere in what you’re returning you can’t. If you add self to the table you’re returning in the constructor it would be as easy as just indexing it.

function module.constructor()
    local self = {}
    return setmetatable({self = self}, module)
end

function othermodule.constructor()
    local class = module.constructor()
    local classesSelf = class.self
end
1 Like

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