Object Oriented Programming Issue

So what happened today was that I was following a well known tutorial made by @magnalite, however I stumbled upon an issue that affected the whole module. I’ve tried for hours looking for a solution, but not much ideas crossed my mind, and every idea I’ve tried to do has not been successful. The issue is in the rogue class module, and the issue says the following:

here is the code for the character class module:

local character = {}


character.__index = character



function character.new(health, armor, speed)
	local newcharacter = {}
	setmetatable(newcharacter, character)
	
	newcharacter.health = health
	newcharacter.armor = armor
	newcharacter.speed = speed
	
end

return character

And then here is the rogue class:

local Character = require(script.Parent)
local RogueClass = {}

RogueClass.__index = RogueClass

setmetatable(RogueClass, Character)

function RogueClass.new(health, armor, speed, ability)
	local newrogue = Character.new(health, armor, speed)
		setmetatable(newrogue, RogueClass) -- this in specific is the issue
	newrogue.Ability = ability
	function RogueClass:DealDamage(initaldamage)
		health = health - initaldamage
		print("Dealt Damage: ".. initaldamage)
		
	end
	return newrogue
end

return RogueClass

Edit: I’ve also posted on the object oriented programming topic but no one replied so I felt like I’d have a much better chance on a solution if I made a topic on the issue.

Well in the error its saying its expecting a table but whatever your putting into the args is nil

You never returned newcharacter in your character.new constructor.

2 Likes

Never thought about that, however when I tried it, it still didn’t work. However thanks for solving that. It helped a lot

As incapaz said since you didnt return the constructor its returning nil,

EDIT : Have you found a solution yet

No, not yet, however I am still looking for one. Also thanks for helping me because I still couldn’t find the solution yet ):

Well you could try to setup your constructor differently

hmm, I still could try to do that. It might work, but I am keeping my fingers crossed on that idea

How did return newcharacter not solve the problem?

Also

What you are doing here is, every time you call your constructor you create a new :DealDamage method.

You don’t want to do this, so just move it outside of your constructor

4 Likes

Here I think this will help you, there are different ways of doing OOP and I adopted one that is very easy too use and read

local character = {}

function character.new(health,armor,speed)
      health = health or 0;
      armor = armor or 0;
      speed = speed or 0;
      return {

       -- methods in here


    }
end

return character

Wait a second, what you did was really the solution. Thank you so much I didn’t know the secondary function could affect everything! :relieved:

Hmm, that sounds like a good idea too, I might try and do that once I learn modern lua oop methods.

1 Like

Wait, but also quick question. I know I already have the solution, but because I took out the dealdamage constructor I don’t know how to really edit the value, health when the function isn’t in the constructor. Any solutions?

What do you mean when it isn’t in the constructor

Ok when I mean it isn’t in the constructor I mean I don’t know how to get values such as health, armor, and walkspeed and put it in somewhere outside of the constructor. If you can tell me how please do.

Should I make it a variable outside of the scripts so that I can access those values?

I highly suggest you try the method of OOP, I showed you it works very well and is very similar to the style everyone else uses!

CHARACTER CLASS

local character = {}


function character.new(health,armor,speed)
	health = health
	armor = armor
	speed = speed
	return {
		
		GetSpeed = function(self)
			print(speed)
		end;
		
		GetHealth = function(self)
			print(health)
		end
		
		
	}
end



return character

ROGUE CLASS

local character = require(game.ServerStorage.Character)


local rogueclass = {}


function rogueclass.new(health,armor,speed,ability)
	ability = ability
	local rogue = character.new(health,armor,speed)
	
	rogue.DealDamage = function(self,damage)
		health = health - damage
		print(health)
	end
	
	return rogue
end

@scripting1st It inherits methods from the character constructor!

1 Like

Wow thank you so much. It really helps me out by how much you are contributing to this post! I’m totally using your method now!

1 Like

Im glad that youve chosen to use my method, it’s really the most basic type of OOP but it still very active, youll really love it! If you need any help Ill be right here

I think your method is amazing, recently tried it out and it works! Thank you so much! You really changed the way I viewed oop! :smile:

1 Like

Dude thats literally amazing to hear and you can use this for anything youd like and also, remember just because something is complicated doesnt mean its better to use!

EDIT : @scripting1st Also, your the second person to ever use this method!

1 Like