You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to give data about things like objects through classes.
What is the issue? Include screenshots / videos if possible!
I have a basic understanding of classes and metatables, but why should I use them?
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have looked on different posts and about how they explain classes, but not why you should use them.
local mod = {}
function mod.RetrieveData()
local DataClass = setmetatable(mod, {})
DataClass.Message = "Hello World!"
return DataClass
end
return mod
First of all, dot is constructor, use it to create new object, if you want methood then use semicolon
Second, OOP is only code writing style, it have small usage in most cases on Roblox, personally i tried to overuse OOP and it was worse
Classes and Objects are structures from other languages that should be used only if you have a lot of invidual objects working on their own, other than that you should use Practices of OOP and normal modules to create your code
Not exacly, semi colon is used in methoods, for code redability it’s used commonly in normal modules, but in OOP dot is constructor where you define self as new object, and inside methood you use self as this object you edit
local Functions = {}
Functions__index = Functions
function Functions.new(name: string, age:number)
local self = setmetatable({},Functions) -- we create our self metatable
self.Name = name
self.Age = age
return self
end
function Functions:ChangeName(newName: string)
self.Name = newName -- some example function, self is object that we call
--so no need for defining it
end
function Functions:Destroy()
self.Name = nil
self.Age = nil
setmetatable(self,nil)
end
Back to your question, you can use OOP but it’s not reccomended if you don’t work with invidual objects like entitites that have specific functions for example function that makes some enemy shoot at specific thing, then object way is great idea because you can control each with specific functions
Thank you (have to type more because of the text limit thing)
So, only use OOP when you’re dealing with multiple objects that have the same purpose.
Like in my game, I have a group of rings the player collects:
-- module
local mod = {}
function mod:RingProperties()
local self = setmetatable(mod, {})
self.Name = "Rings"
self.Value = 1
return self
end
return mod
-- main script
local Module = require(script.Module)
local var = Module:RingProperties()
print(var) -- Prints {"Rings", 1}