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:
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.
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, 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?
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!
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
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!