Help with OOP Module

Hello,

I am making a combat melee game, and I have a module that can create new weapons and set properties and etc… Problem is, I don’t have much experience in OOP. setmetatable doesn’t even work in my script, and self doesn’t work at all. I’m not asking people to revise my script, but to just tell me how to use self and setmetatable, and how I am doing it wrong.

weapon_settings = {}
weapon_settings.__index = weapon_settings
local config = require(game:GetService("ReplicatedStorage"):FindFirstChild("WeaponConfig"))

local PROPERTY_NOT_FOUND = "%s property not found in %s"

local WEAPON_NOT_FOUND = "%s weapon not found!"

local MODEL_NOT_FOUND = "%s model not found!"

function weapon_settings.new_weapon(name: string?, price: number?, damage: number?, isShiny: boolean?, normal_version: string?, description: string?, rarity: string?)
	local newConfig = setmetatable({
		ID = name,
		Damage = damage,
		Model = nil,
		Price = price,
		IsShiny = isShiny,
		NormalVersion = normal_version,
		Description = description,
		Rarity = rarity,
	},weapon_settings)
	config.Melee[name] = newConfig
	return newConfig
end
function weapon_settings:SetModel(model: Model?, weapon: string?)
	local configGun = config.Melee[weapon]
	if configGun then
		if model:IsA("Model") and model:FindFirstChild("Handle") then
			configGun.Model = model
    			--self[weapon .. "Model"] = model
			return configGun
		else
			error(string.format(MODEL_NOT_FOUND,model.Name))
		end
	else
		error(string.format(WEAPON_NOT_FOUND,weapon))
	end
end

function weapon_settings:SetProperty(property: string?, value: any?)
	if self[property] ~= nil then
		self[property] = value
	else
		error(string.format(PROPERTY_NOT_FOUND,property,self.ID))
	end
end
return weapon_settings

Still wondering about this, OOP seems too hard for me. Updated the code:

weapon_settings = {}
weapon_settings.__index = weapon_settings
local config = require(game:GetService("ReplicatedStorage"):FindFirstChild("WeaponConfig"))

local PROPERTY_NOT_FOUND = "%s property not found in %s"

local WEAPON_NOT_FOUND = "%s weapon not found!"

local MODEL_NOT_FOUND = "%s model not found!"

function weapon_settings.new_weapon(name: string?, price: number?, damage: number?, isShiny: boolean?, normal_version: string?, description: string?, rarity: string?)
	local newConfig = {
		ID = name,
		Damage = damage,
		Model = nil,
		Price = price,
		IsShiny = isShiny,
		NormalVersion = normal_version,
		Description = description,
		Rarity = rarity,
	}
	setmetatable(newConfig,weapon_settings)
	config.Melee[name] = newConfig
	return newConfig
end
function weapon_settings:SetModel(model: Model?, weapon: string?)
	local configGun = config.Melee[weapon]
	if configGun then
		if model:IsA("Model") and model:FindFirstChild("Handle") then
			configGun.Model = model
    			--self[weapon .. "Model"] = model
			return configGun
		else
			error(string.format(MODEL_NOT_FOUND,model.Name))
		end
	else
		error(string.format(WEAPON_NOT_FOUND,weapon))
	end
end

function weapon_settings:SetProperty(property: string?, value: any?)
	if self[property] ~= nil then
		self[property] = value
	else
		error(string.format(PROPERTY_NOT_FOUND,property,self.ID))
	end
end
return weapon_settings

Ah, I fixed it on my own. I learned what self was. I just set the newconfig to self and then returned the metatable… wow, it was that easy!

I’d suggest looking at this:

For a clear great rundown on OOP, and how to use self as well.
I’m sure you’re grasping it now, but this should make it so much clearer.

I learned from material like this, so I’d suggest it!

I already looked at the post, it didn’t really help that much, but thank you for the help!

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