Why should I use classes to set data about objects and not just normal scripts?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to give data about things like objects through classes.

  2. What is the issue? Include screenshots / videos if possible!
    I have a basic understanding of classes and metatables, but why should I use them?

  3. 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
1 Like

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

1 Like

Suppose I say:

self = mod -- Are they equivalent?

is this the same as referencing the table itself?

If they are, it makes a lot more sense, because your just saying

mod.This = 5
mod.That = true

-- same as

self.This = 5
self.That = true
1 Like

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
1 Like

Apologies, but why do you type it like this?

name : string
or 
age : number

oh nevermind i think i see why, but you can continue with your own explanation :+1:

1 Like

those are types, i use them in functions to make it clear what i have to pass, i advice you using them

1 Like

That makes a lot of sense now.

Are there any posts about them?

1 Like

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

Also i made manual, 11th topic is about types: Scripting Manual

1 Like

Thank you :+1: (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}

1 Like

Right, and when they need to use this purpose invidually, like entity tracking player or pets

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.