Am I doing this correctly? OOP

I don’t think that I’m making this skill tree system correctly. I’m working on a one piece game, and I’d like to know if I’m doing anything wrong. I think there may be a problem with the way I set up the functions. Thanks!

local module = {}

function module:PlayerSkillTree()
	
	local skillTrees={
		["Rokushiki"]={
			{skillid = 1, skillname = "Soru", requirements={}},
			{skillid = 2, skillname = "Tekkai", requirements={"Soru"}},
			{skillid = 3, skillname = "Geppo", requirements={"Tekkai"}},
			{skillid = 4, skillname = "Rokuogan", requirements={"Geppo"}},
		},
		["Fishman Karate"]={
			{skillid = 1, skillname = "Water Bullets", requirements={}},
			{skillid = 2, skillname = "Shark Brick Fist", requirements={"Water Bullets"}},
		},
		["Boxing"]={
			{skillid = 1, skillname = "Trained Boxer", requirements={}},
			{skillid = 2, skillname = "Gloves", requirements={"Trained Boxer"}},
			{skillid = 3, skillname = "Right Hook", requirements={"Gloves"}},
			{skillid = 4, skillname = "King Punch", requirements={"Right Hook"}}
		},
		["Blackleg"]={
			{skillid = 1, skillname = "Diable Jambe", requirements={}},
			{skillid = 2, skillname = "Party Table", requirements="Diable Jambe"},
			{skillid = 3, skillname = "Concasser", requirements="Party Table"},
		}
	}

	function module.getTree(treename)
		return skillTrees[treename]
	end
	function module.getDefaults()
		return {skillTrees["Rokushiki"], skillTrees["Fishman Karate"], skillTrees["Boxing"]}
	end
	function module.acquireSkill(player, skill)
		local requirementmet = true
		local style
		for treestyle,tree in pairs(skillTrees) do
			for _, skill in pairs(tree) do
				if skill.skillname == skill then
					local requirements = skill.requirements
					for _,requiredskill in pairs(requirements) do
						if player.Backpack:FindFirstChild(requiredskill) then
							style = treestyle
						else
							requirementmet = false
						end
					end
				end
			end
		end
		if requirementmet then
			if not player.Style.Value and style then
				player.Style.Value = style
			end
			local skill = game.ServerStorage.Tools:FindFirstChild(skill):Clone()
			skill.Parent = player.Backpack
		end
	end
	
end

return module
1 Like

What seems to be wrong about it? BTW you can create a custom value type so you don’t have to write out all the parameters and arguments like that for the skills.

This isn’t necessarily a problem per se but functions where the subject object, i.e; object:Function() is not referenced within the function’s body (as ‘self’) may as well be defined with the dot operator, i.e; object.Function(). In other words if the colon operator is used and the implicit parameter ‘self’ goes unused then the dot operator should be used instead.

local object = {}

function object.Function()
	print(self) --nil
end

function object:Method()
	print(self) --Prints 'object'.
end

return object
1 Like