#modulescript not working with self

112 messages · Page 1 of 1 (latest)

granite halo
#

i just started using OOP and recently the "self" is confusing, is self the first argument passed from the function?? or is self the first variable in the table . i looked in the devforum but it didnt make sense to me. any help is appreciated

jovial sequoiaBOT
#

studio** You are now Level 1! **studio

eternal ledge
#

the self is the object

#

imagine an car

#

the self is the car

granite halo
#

I see, how do you use self for other functions? I tried it for printsystem() it just printed (“can’t add arthemical value”)

#

Even though I have gas and rocks set to 20 and 10

eternal ledge
#

you are setting the printSystem wrong

granite halo
#

what’s wrong with it?

eternal ledge
#

you have to put in the module function self:PrintSystem

#

not NewGenTable

granite halo
#

o

eternal ledge
#

because u want to see the gas and rocks of an object

#

so the function is child of the self(object)

#

not the class(NewgenTable)

granite halo
#

so we call the table inside the constructor?

#

hm

eternal ledge
#

and other thing

#

u need to set the object to a variable

#

like this

granite halo
#

Hold ima try that

eternal ledge
#

var = Class:new()

#

and call var:PrintSystem

#

the : is for reference the object automatic

granite halo
#

Ok I fixed that above

eternal ledge
#

an recomendation for you is to learn type annotation

#

it helps a lot in OOP

granite halo
#

Alright ill look into that

#

still saying some error

eternal ledge
#

u need to put in the construtor

jovial sequoiaBOT
#

studio** You are now Level 3! **studio

granite halo
#

O

eternal ledge
#

its not that

granite halo
#

Print system?

eternal ledge
#

wait a sec

#

ill show u

granite halo
#

Alright thanks

#

it’s confusing

jovial sequoiaBOT
#

studio** You are now Level 2! **studio

eternal ledge
#
NewGenTable = {}
NewGenTable.__index = NewGenTable

-- Construtor
function NewGenTable.new(gas, rocks, shards)
    local self = setmetatable({}, NewGenTable)
    self.gas = gas
    self.rocks = rocks
    self.shards = shards

    function self:PrintSystem()
        print(self.gas + self.rocks)
    end

    return self
end


return NewGenTable
#

done

#

and in the other script u do:

granite halo
#

AH

#

I didn’t return self

#

and used constructor instead instead of self

eternal ledge
#
randomTable = require(LOCATION)

local obj = randomTable.new(10,10,10)
obj:PrintSystem()
#

any questions now?

granite halo
#

honestly I’ve been using constructor then the function for everything

#

just what I saw on the dev forum

#

I appreciate you showing me self function

eternal ledge
#

i can recomend u a OOP starters video if u want

granite halo
#

Sure

eternal ledge
granite halo
#

I have one question for self:printsystem, why don’t we use NewGenTable:PrintSystem instead?

eternal ledge
#

its old but for starters works well

granite halo
#

That’s a good playlist

eternal ledge
#

i gonna draw a quick ex

granite halo
#

alright

eternal ledge
#

i didnt explaned to well

granite halo
#

its ok is the car the self?

eternal ledge
#

honestly i cant explain too much because i dont know how can explain in a easy way

eternal ledge
granite halo
#

so

#

self isnt defined

#

untilyou define it

#

blueprint holds data

#

aka constructor

eternal ledge
#

correct

#

In all of my object oriented programming videos, I use self. Its a keyword that makes your code more readable and understandable, but doesn't change anything. In this video, I explain how it works. Hope you enjoy!

Object Oriented Programming Roblox: http://bit.ly/2XQfWcG

Socials:

▶ Play video
granite halo
#

Im kinda getting it

eternal ledge
#

self represents the object

granite halo
#

can self be anything

eternal ledge
#

(i think)

granite halo
#

Ok

#

you can you use self in other functions because its connected to the constructor?

#

example

#

construcot has car properties

eternal ledge
#

in the constructor holds the functions and variables, self in this case represent the table holding the things

granite halo
#

and you create a function for the car like speed:() , but you need the car properties so you have to use self to pass the data onto it

#

ah

#

ok

eternal ledge
#

in lua OOP is just tables

#

and metatables

granite halo
#

yep

#

Alr thanks for helping me

#

ill def watch those videos soon

#

and annotions

eternal ledge
#

for annotations it just helps writing

#

like the autocomplete

granite halo
#

hm

jovial sequoiaBOT
#

studio** You are now Level 3! **studio

eternal ledge
#

if u need help with anything ping me and i will try to respond

granite halo
#

alright

#

thanks for everything

eternal ledge
#

have a good day

granite halo
#

you too its currently 12pm lol

#

cya

eternal ledge
granite halo
#

Hey bro its starting to make much more sense to me

#

self is fairly easy to use

#

I think my problem was calling the functions from the scripts

#

self is pretty much what you set it to, its just a way to pass information onto another function without redefining mutiple times.

eternal ledge