Person = {} Person.__index = Person Person.martialArts = {Karate = {}} function Person.new(name, age, eth) local newPerson = { Name = name, Age = age, Ethnicity = eth, martialArts = "Karate" } return setmetatable(newPerson, Person) end function Person.martialArts.Karate:Kick() if self.martialArts == "Karate" then print("Kick!") else return 0 end end function Person:Swim() print(self.Name.. " is swimming!") end return Person
This is my running script:
Person = require(game.ReplicatedStorage.OOP) local John = Person.new("John", "Young", "Human") print(John.martialArts:Kick())
I get an error:
attempt to call method 'Kick' (a nil value)
I want to basically be able to do John.martialArts:Kick() and in that function it will check if he actually has the martial arts "Karate" and if he does, it will execute. I can't find any examples online about this and I've tried but I don't know how to get it to do that (what I'm trying to do). Any idea/help?
I want to basically be able to do John.martialArts:Kick() and in that function it will check if he actually has the martial arts "Karate" and if he does, it will execute
Since it needs to check if John is a certain way, it should be a method of John and not of one of John's fields:
function Person:Kick() if self.martialArts == "Karate" then print("Kick!") else return 0 end end john = Person.new("John", "Young", "Human") john:Kick() --> Kick!
Methods either act upon an object or ask that an object act. In this case, you're asking John to Kick, so it should be a method on John. Since John is a Person, that means you need a Kick method defined for the Person class.
A few things were wrong. First, you forgot to put ".Karate" on line 3, it should be:
Person = require(game.ReplicatedStorage.OOP) local John = Person.new("John", "Young", "Human") print(John.martialArts.Karate:Kick())
Next, the reason why John.martialArts.Karate:Kick() does not work is because martialArts is not a table, you actually set it to a string in the Person.new function.
Person = {} Person.__index = Person Person.martialArts = {Karate = {}} function Person.new(name, age, eth) local newPerson = { Name = name, Age = age, Ethnicity = eth, --martialArts = "Karate" --Don't put this here or give this a different name!! } return setmetatable(newPerson, Person) end function Person.martialArts.Karate:Kick() if self.martialArts == "Karate" then--Also if you changed the string from martialArts to something else, don't forget to change it here too. print("Kick!") else return 0 end end function Person:Swim() print(self.Name.. " is swimming!") end return Person
That's all that you did wrong, it should work if you fix these.