I’m attempting to conquer Metatables and learn OOP so I’m trying it for a small project. I think I’ve got decent progress so far, however, I’ve gotten stuck.
This represents the problem that I’m having:
--Module Script
local question = {}
question.__index = question
function question.Create()
local newQuestion = {}
setmetatable(newQuestion, question)
newQuestion.Property1 = "exampleA" --I define some properties in this function
newQuestion.Property2 = "exampleB"
return newQuestion
end
function question:DoStuff() --I can't access the property here
print(self.Property1) --nil
end
return question
--LocalScript
local questionModule = require(path.to.module)
local question1 = questionModule.Create()
--I can access the properties fine in this script
print(question1.Property1) -- "exampleA"
question1:DoStuff()
My actual code if you need it
Basically, I defined some properties of newQuestion
in the Question.Create()
function of the module script. Those properties are needed for future reference and they aren’t there when I try to use them from the Question:ToggleVisibility()
function.
I’ve removed most of the stuff that isn’t related to ease bloat:
--ModuleScript
Question = {}
Question.__index = Question
function Question.Create(QAProperties)
local newQuestion = {}
setmetatable(newQuestion, Question)
QuestionCount = QuestionCount + 1
newQuestion.Question = QAProperties.Question
newQuestion.DefaultTransparency = QuestionCount % 2 == 0 and 1 or 0.75
newQuestion.IsVisible = true
--This is creates a Frame and some TextLabels, and formats them properly
newQuestion.QuestionObject = CreateQuestionObject(QAProperties.Question)
newQuestion.QuestionObject.Parent = QAObjectsHolder --Folder that holds all the questions
AnswerCount = AnswerCount + 1
newQuestion.AnswerProperties = QAProperties.AnswerProperties
--Same thing here, but for the answer
newQuestion.AnswerObject = CreateAnswerObject(QAProperties.AnswerProperties)
newQuestion.AnswerObject.Parent = QAObjectsHolder
--Returns a Vector2 of the size after some calculations
newQuestion.AnswerSize = CalculateAndSetAnswerSize(newQuestion.AnswerObject)
Question:ToggleVisibility()
return newQuestion
end
function Question:ToggleVisibility()
if self.IsVisible then -- These are nil
self.IsVisible = false
self.QuestionObject.BackgroundColor3 = Color3.new(0, 0, 0) --Errors because of nil
self.QuestionObject.BackgroundTransparency = self.DefaultTransparency
local Tween = TweenService:Create(self.AnswerObject, TweenInfo, {Size = self.AnswerSize})
Tween:Play()
Tween.Completed:Wait()
else
self.IsVisible = true
self.QuestionObject.BackgroundColor3 = Color3.new(1, 1, 1)
self.QuestionObject.BackgroundTransparency = 0.9
local Tween = TweenService:Create(self.AnswerObject, TweenInfo, {Size = self.AnswerSize})
Tween:Play()
Tween.Completed:Wait()
end
end
return Question
--LocalScript
--Require the module from above
local FQM = require(script.Parent.FAQQuestionsModule)
--//Questions & Answers
local Q1, A1 = FQM.Create({
Question = "Question Prompt",
AnswerProperties = {
TextLabel = {
AnchorPoint = Vector2.new(0.5, 0.5),
--etc...
}
}
})
--I can access all of the properties here...
local Questions = {Q1, Q2, Q3, Q4, Q5, Q6, Q7}
local Answers = {A1, A2, A3, A4, A5, A6, A7}
--//Button Activations
local function ButtonActivated(QObject)
QObject:ToggleVisibility()
end
for _, QObject in ipairs(Questions) do
QObject.QuestionObject.ActivationButton.Activated:Connect(function()
ButtonActivated(QObject)
end)
end
This has been bothering me for some time now, and I’m assuming I just don’t understand something