#OOP (my self is returning as nil

362 messages · Page 1 of 1 (latest)

grave sparrow
#

1st one is module script, 2nd one is server script

vale mango
#

I cant see the pic well, but you seem to be calling the GetVar with dot instead of : which dosent pass in self

#

so either pass in self or use num:GetVar()

grave sparrow
#

wdym pass in self?

#

can you explain?

vale mango
#

so when you use the dot notation

vale apex
#

no, you need to set the metatable

vale mango
#

or when you use the colon notation, it basiacly passes in the self in the function

vale apex
#

return setmetatable(MT, self)

#

ª

vale apex
#

didn't say nothing

#

xd

#

ye u just need to call the method with colons

#

:

#

num:GetVar()

grave sparrow
vale apex
#

try the above

vale mango
#

you can do a num:GetVar() which is the same thing as num.GetVar(self)

#

its just syntactic sugar

vale apex
#

yep

grave sparrow
#

now im getting 20:03:18.372 ServerScriptService.Script:9: attempt to call missing method 'GetVar' of table - Server - Script:9

vale mango
#

did you change anythin in the module?

grave sparrow
#

I put setmetatable(MT, self)

vale mango
#

just return self like u did

#

and it would be setmetatable(self, MT)
right

grave sparrow
#

ik but Izan told me to change it around

vale mango
#

you wanna link self to the meta table

#

so the first paramter is the table and the sec is the meta

grave sparrow
#

ok it got rid of the error but my self is still nil

vale mango
#

show pic of module rq

grave sparrow
#
local MT = {}
MT.__index = MT



function module.new(var1, var2)
    local self = {}
    setmetatable(self, MT)
    self.var = var1 or nil
    self.var = var2 or nil
    return self
end


function MT:GetVar()
    print(self)
end




return module```
vale mango
#

print self?

grave sparrow
#

wait mb

#

OMG

#

IM SO DUMB

#

bro

#

lol

#

I set the self.var for the other self.var

#

so it was returning nil

#

I just had to do

#

self.new_var = var1 or nil
self.new_var2 = var2 or nil

vale mango
#

did u put two values?

#

it would override the first value but should atleast print the seconed

grave sparrow
#

works but

#

when I was using OOP and making my functions sometimes my self would go nil

#

how do I fix that?

#

Do I just makes checks for self in my MT:Functions?

vale mango
#

did you use dot notation?

grave sparrow
#

for my functions?

#

MT?

vale mango
#

ye when you call them

grave sparrow
#

well I use :

#

so no

vale mango
#

when you made them in the module did you make em with : too

grave sparrow
#

yes

#

I do MT:MyFunction()

vale mango
#

do you have an example you can show?

grave sparrow
#

hold up

#

not really cause I deleted the module cause I gave up but I had it to where it would type a self.string and tywrite it on a textlabel but i did local len = string.len(self.thread) and it kept saying that self.thread was nil and it wasnt a string

#

unless I did something wrong?

vale mango
#

its hard to understand without code

#

like where did the self.thread come from

grave sparrow
#

from my constructor

vale mango
#

can you try to replicate the code you just talked abt

#

like just do a quick constructer and method

grave sparrow
#

alr

#

give me a sec

#

something like this:

local MT = {}
MT.__index = MT



function module.new_string(new_string)
    local self = {}
    setmetatable(self, MT)
    self.new_thread = new_string  or "Lorem"
    return self
end


function MT:TypeWrite()
    local len = string.len(self.new_thread)
    print(len)
end



return module ```
#

@vale mango

vale mango
#

did you run it?

#

it worked for me

grave sparrow
#

it works now for some reason but before it kept saying attempt to index nil with thread

#

maybe I put something wrong the last time?

vale mango
#

maybe

grave sparrow
#

oh wait a second I have the orginal script hold up I just remembered

vale mango
#

alr

grave sparrow
#

This is the orginal script:

local tw = {}
sh.__index = sh
tw.__index = tw


function sh.new_s(new_string, new_libary)
    local self = setmetatable({}, sh)
    self.string = new_string or "Lorem"; if type(new_string) ~= "string" then return error("Argument provided is not a string or did not provide a string!") end 
    self.stable = new_libary or {}; if type(new_libary) ~= "table" then return error("Argument provided is not a table or did not provide a table!") end 
    return self
end

function sh.new_threadObject(thread_object, new_thread, speed, sound, typewrite)
    local self = setmetatable({}, tw)
    self.object = thread_object or nil; if thread_object.ClassName ~= "TextLabel" then return error("Argument provided is not a TextLabel or did not provide one! ") end
    self.thread = new_thread; if type(new_thread) ~= "string" then return error("Argument provided is not a string or did not provide one!") end
    self.thread_speed = speed or 0; if type(speed) ~= "number" then return error("Argument provided is not a number or did not provide one!") end
    self.thread_sound = sound; if sound.ClassName ~= "Sound" then return error("Argument provided is not a Sound or did not provide one!") end 
    self.write = typewrite or nil; if type(typewrite) ~= "boolean" then return error("Argument provided is not a boolean or did not provide a boolean!") end
    return self 
end

local function playSound(sound)
    coroutine.create(playSound)
    if sound.IsLoaded ~= true then return warn("Sound is loading or has not loaded") end
    sound.Loaded:Connect(function()
        sound:Play()
    end)
end

function sh:play_thread()
    local len = string.len(self.thread)
    for i = 1, len, 1 do
        self.object.Text = string.sub(self.thread, 1, i)
        task.wait(self.thread_speed)
    end
end

return sh ```
error in the output: 13:14:52.238  ReplicatedStorage.Modules.sh:33: invalid argument #1 to 'len' (string expected, got nil)  -  Client - sh:33
vale mango
#

eh

#

how did u use it

grave sparrow
#

wdym?

#

I couldn't cause it didn't work crying

vale mango
#

in the script you rquired

#

ye jus gimme how you did it

grave sparrow
#

oh

#

you mean In the server script?

vale mango
#

ye

grave sparrow
#

one sec

vale mango
#

btw

#

self.thread

#

is not in sh

grave sparrow
#

o

#

m

vale mango
#

no holdon

grave sparrow
#

g

#

no way

vale mango
#

lemme look at this again

grave sparrow
#

oh alr

vale mango
#

can I have the example where you put the paramters

#

I cant bother to use it myself

grave sparrow
#

sure

#

one sec

#

yea def dont use it cause its broken lol

vale mango
#

ye am just sayin show me how you used it so I can see how it broke

#

do you not have the server script where you rquired it

grave sparrow
#

something like this:


local mod = require(RS:WaitForChild("module1"))

local String = "hello"
local TL = script.Parent.TextLabel
local Sound = "Sound"

mod.new_threadObject(TL, String, 0.03, Sound, true)

mod:play_thread() ```
#

but it didnt work cause of the error above

#
    local len = string.len(self.thread)
    for i = 1, len, 1 do
        self.object.Text = string.sub(self.thread, 1, i)
        task.wait(self.thread_speed)
    end
end ```

This function right here was the main function that was giving me problems
vale mango
#

are you sure its not cuz the playthread method dosent exist in the metatalbe you linked it to

#

which is tw

#

I canhed it to sh

#

Changed it

#

local self = setmetatable({}, sh)
in new_threadObject

grave sparrow
#

ohh

#

wait

#

so are you saying I was supposed to do

#

function tw:play_thread?

vale mango
#

yes

#

then it would find the function

grave sparrow
#

let me try that then

#

let yk if it works

#

now its saying

#

OMG

#

@vale mango

#

BRO

#

IT WORKS

#

OMG

#

TY

#

for replacing my braincells

vale mango
#

np

grave sparrow
#

wanna try it ouy?

vale mango
#

hm?

grave sparrow
#

Ill give you the newer version

vale mango
#

sure

grave sparrow
#
local tw = {}
sh.__index = sh
tw.__index = tw


function sh.new_s(new_string, new_libary)
    local self = setmetatable({}, sh)
    self.string = new_string or "Lorem"; if type(new_string) ~= "string" then return error("Argument provided is not a string or did not provide a string!") end 
    self.stable = new_libary or {}; if type(new_libary) ~= "table" then return error("Argument provided is not a table or did not provide a table!") end 
    return self
end

function sh.new_threadObject(thread_object, new_thread, speed, sound, typewrite)
    local self = setmetatable({}, tw)
    self.object = thread_object or nil; if thread_object.ClassName ~= "TextLabel" then return error("Argument provided is not a TextLabel or did not provide one! ") end
    self.thread = new_thread; if type(new_thread) ~= "string" then return error("Argument provided is not a string or did not provide one!") end
    self.thread_speed = speed or 0; if type(speed) ~= "number" then return error("Argument provided is not a number or did not provide one!") end
    self.thread_sound = sound; if sound.ClassName ~= "Sound" then return error("Argument provided is not a Sound or did not provide one!") end 
    self.write = typewrite or nil; if type(typewrite) ~= "boolean" then return error("Argument provided is not a boolean or did not provide a boolean!") end
    return self 
end

local function playSound(sound)
    coroutine.create(playSound)
    if sound.IsLoaded ~= true then return warn("Sound is loading or has not loaded") end
    sound.Loaded:Connect(function()
        sound:Play()
    end)
end

function tw:play_thread()
    local len = string.len(self.thread)
    for i = 1, len, 1 do
        self.object.Text = string.sub(self.thread, 1, i)
        task.wait(self.thread_speed)
    end
end

return sh ```
#

only problem is I dont see play_thread for auto completion when I do


new_thread:play_thread() ```
vale mango
#

thing is I cant test it out, cuz I dont have a sound

#

but holdon, lemme remove some stuff

grave sparrow
#

just search in toolbox for one?

#

and paste into Sound Service

grave sparrow
#

dangit

#

sound is not working

#

but ik

#

why

#

Think I can fix

grave sparrow
vale mango
#

local self = setmetatable({}, tw)

#

this line

#

local self = {}

#

setmetatable({}, tw)

#

so change this local self = setmetatable({}, tw)

#

into this local self = {}
setmetatable(self, tw)

grave sparrow
#

alr

#

like this function sh.new_threadObject(thread_object, new_thread, speed, sound, typewrite) local self = {} setmetatable(self, tw) self.object = thread_object or nil; if thread_object.ClassName ~= "TextLabel" then return error("Argument provided is not a TextLabel or did not provide one! ") end self.thread = new_thread; if type(new_thread) ~= "string" then return error("Argument provided is not a string or did not provide one!") end self.thread_speed = speed or 0; if type(speed) ~= "number" then return error("Argument provided is not a number or did not provide one!") end self.thread_sound = sound; if sound.ClassName ~= "Sound" then return error("Argument provided is not a Sound or did not provide one!") end self.write = typewrite or nil; if type(typewrite) ~= "boolean" then return error("Argument provided is not a boolean or did not provide a boolean!") end return self end ?

vale mango
#

nvm, now am confused

grave sparrow
#

oh

grave sparrow
vale mango
#

Am tryna see why it aint autocompletin

grave sparrow
#

ohh

#

alr

#

am getting this now

#
    coroutine.create(playSound)
    if sound.IsLoaded ~= true then return warn("Sound is loading or has not loaded") end
    sound.Loaded:Connect(function()
        sound:Play()
    end)
end

function tw:play_thread()
    local len = string.len(self.thread)
    for i = 1, len, 1 do
        self.object.Text = string.sub(self.thread, 1, i)
        coroutine.resume(playSound(self.thread_speed))
        task.wait(self.thread_speed)
    end
end

return sh ``` -   20:56:43.357  ReplicatedStorage.module1:26: attempt to index number with 'IsLoaded'  -  Client - module1:26
vale mango
#

not so much famliar with sound

#

but did that happen after the change

grave sparrow
#

Yea..

#

should I use task.spawn instead?

vale mango
#

so this is a bit diffrent from urs but it autocompletes for me

#

and wait lemme see what u tryna do with that coroutine

#

the coroutine create needs to be followed with coroutine resume to start it

#

or if u want do task.spawn to spawn the thread

grave sparrow
#

I did this ``` local function playSound(sound)
coroutine.create(playSound)
if sound.IsLoaded ~= true then return warn("Sound is loading or has not loaded") end
sound.Loaded:Connect(function()
sound:Play()
end)
end

function tw:play_thread()
local len = string.len(self.thread)
for i = 1, len, 1 do
self.object.Text = string.sub(self.thread, 1, i)
coroutine.resume(playSound(self.thread_sound))
task.wait(self.thread_speed)
end
end

return sh ``` Error: 21:02:25.418 ReplicatedStorage.module1:37: missing argument #1 to 'resume' (thread expected) - Client - module1:37

vale mango
#

with coroutine.resume you need a refernce of the coroutine

#

the one you made with .create

grave sparrow
#

oh

#

wait

#

but why does it say thread expected (I think im just tired im sorry lol)

#

I lose braincells when im tired

#

let me figure this out rq

vale mango
#

am not sure why I cant do a coroutine in a module

#

its not autocompletin

grave sparrow
#

oh wait

#

maybe share permissions?

#

wait no

vale mango
#

try this

grave sparrow
#

alr

vale mango
#

but is the previous problem solved?

grave sparrow
#

yea

#

ty

vale mango
#

so this is a new one

grave sparrow
#

🙂

#

One that I made crying

vale mango
#

o oke

grave sparrow
#

sorry

#

fixing it though

vale mango
#

lol, no worry

#

its how you learn

grave sparrow
#

Alr

#

No error anymore

#

but

#

sound isnt playing

vale mango
#

?

#

am not familar with sounds

grave sparrow
#

Updated version ``` local sh = {}
local tw = {}
sh.__index = sh
tw.__index = tw

function sh.new_s(new_string, new_libary)
local self = setmetatable({}, sh)
self.string = new_string or "Lorem"; if type(new_string) ~= "string" then return error("Argument provided is not a string or did not provide a string!") end
self.stable = new_libary or {}; if type(new_libary) ~= "table" then return error("Argument provided is not a table or did not provide a table!") end
return self
end

function sh.new_threadObject(thread_object, new_thread, speed, sound, typewrite)
local self = {}
setmetatable(self, tw)
self.object = thread_object or nil; if thread_object.ClassName ~= "TextLabel" then return error("Argument provided is not a TextLabel or did not provide one! ") end
self.thread = new_thread; if type(new_thread) ~= "string" then return error("Argument provided is not a string or did not provide one!") end
self.thread_speed = speed or 0; if type(speed) ~= "number" then return error("Argument provided is not a number or did not provide one!") end
self.thread_sound = sound; if sound.ClassName ~= "Sound" then return error("Argument provided is not a Sound or did not provide one!") end
self.write = typewrite or nil; if type(typewrite) ~= "boolean" then return error("Argument provided is not a boolean or did not provide a boolean!") end
return self
end

local function playSound(sound)
if sound.IsLoaded ~= true then return warn("Sound is loading or has not loaded") end
sound.Loaded:Connect(function()
sound:Play()
end)
end

local cor = coroutine.create(playSound)

function tw:play_thread()
local len = string.len(self.thread)
for i = 1, len, 1 do
self.object.Text = string.sub(self.thread, 1, i)
coroutine.resume(cor, self.thread_sound)
task.wait(self.thread_speed)
end
end

return sh ```

vale mango
#

is it like a permission thing maybe

grave sparrow
#

I had this problem before

#

I think

#

It has something to do with the sound being in a for do

#

or maybe

#

it is playing

#

its just

#

waiting?

#

im confused

#

But I wouldn't have to sound permissions cause its not my sound?

#

right?

#

im sure of?

vale mango
#

what are you trying to do with this loop

#

because from what I remembe you can only resume a coroutine once?

grave sparrow
#

oh

vale mango
#

unless u add a yiled

#

in the function

grave sparrow
#

like task.wait()?

#

at the top?

vale mango
#

no a coroutine.yiled in the function

#

play sound

grave sparrow
#

ohhhhhh

#

I forgot about that lol

vale mango
#

am just not sure about the logic

#

what are you trying to do

grave sparrow
#

oh I was trying to

#

well I wanted to play the sound as soon as the text started

#

or the thread/string began at i and the string.sub was equal to -1, -1

#

so I used a corutine.create

#

I was going to use

#

task.spawn

vale mango
#

why?

grave sparrow
#

tbh idk I thought it ment it would start my function when the type writing effect began

#

I think

#

I might have to rewrite

vale mango
#

why do you need a thread for it tho

grave sparrow
#

it

#

wdym?

vale mango
#

why not a normal function

grave sparrow
#

my self.thread or the corutine?

vale mango
#

it is a connection afterall, why you put it in a coroutine

grave sparrow
#

Idk I thought I was cooking there

#

lol..

vale mango
#

LOL

grave sparrow
#

💀

vale mango
#

Thats fuckin funny

grave sparrow
#

we dont talk about this crying

#

I should remove it

#

If it's not really giving me what I want

vale mango
#

ye just a normal function

grave sparrow
#

tbh

vale mango
#

a coruotine thread is needed if you have a function that yileds

vale mango
#

like a function that has a task.wait(2) in it

#

but if its a connection

#

they dont yiled

grave sparrow
#

oh so when I use run service basically ?

vale mango
#

ye

grave sparrow
#

ah ok

#

OH

#

I get it now

#

that was me being slow ngl

vale mango
#

It would help you if u understood how coroutines work like step by step

#

nah, they tricky at first

grave sparrow
#

one problem though

vale mango
#

bro..

grave sparrow
#
local tw = {}
sh.__index = sh
tw.__index = tw


function sh.new_s(new_string, new_libary)
    local self = setmetatable({}, sh)
    self.string = new_string or "Lorem"; if type(new_string) ~= "string" then return error("Argument provided is not a string or did not provide a string!") end 
    self.stable = new_libary or {}; if type(new_libary) ~= "table" then return error("Argument provided is not a table or did not provide a table!") end 
    return self
end

function sh.new_threadObject(thread_object, new_thread, speed, sound, typewrite)
    local self = {}
    setmetatable(self, tw)
    self.object = thread_object or nil; if thread_object.ClassName ~= "TextLabel" then return error("Argument provided is not a TextLabel or did not provide one! ") end
    self.thread = new_thread; if type(new_thread) ~= "string" then return error("Argument provided is not a string or did not provide one!") end
    self.thread_speed = speed or 0; if type(speed) ~= "number" then return error("Argument provided is not a number or did not provide one!") end
    self.thread_sound = sound; if sound.ClassName ~= "Sound" then return error("Argument provided is not a Sound or did not provide one!") end 
    self.write = typewrite or nil; if type(typewrite) ~= "boolean" then return error("Argument provided is not a boolean or did not provide a boolean!") end
    return self 
end

local function playSound(sound)
    if sound.IsLoaded ~= true then return warn("Sound is loading or has not loaded") end
    sound.Loaded:Connect(function()
        sound:Play()
    end)
end

function tw:play_thread()
    local len = string.len(self.thread)
    for i = 1, len, 1 do
        self.object.Text = string.sub(self.thread, 1, i)
        playSound(self.thread_sound)
        task.wait(self.thread_speed)
    end
end

return sh ``` I updated the script sound is still not playing
vale mango
#

:I

grave sparrow
#

🙏

vale mango
#

error pls....??

grave sparrow
vale mango
grave sparrow
#

ah sh

vale mango
#

again why is the function in a loop

#

dont you need to start the sound once?

#

or is it liek a short sound and plays for each letter???

grave sparrow
#

Because If it's not in the loop the sound wont play when the type writer effect yk types

vale mango
#

o its a typewriter...

grave sparrow
vale mango
#

sound.Loaded:Connect(function()
sound:Play()
end)

vocal tundraBOT
#

studio** You are now Level 14! **studio

vale mango
#

this makes it not fire after it loaded

grave sparrow
#

yea but what about my check?

#

if sound.IsLoaded ~= true then return warn("Sound is loading or has not loaded") end

vale mango
#

no I mean becuase it already loaded

#

the event wont fire....

grave sparrow
#

alr let me try

vale mango
#

cuz the event listens for when the sound fires then it plays it

vale mango
#

the listner

#

this

#

sound.Loaded:Connect(function()

end)
grave sparrow
#

OH

vale mango
#

this will listen for when the sound loaded

grave sparrow
#

wait

#

im slow

vale mango
#

but you have a check

#

so it wont ever fire

grave sparrow
#

alr

vale mango
#

plus it only will fire once and after it loaded it wont fire again even if you call the function

grave sparrow
#

It's still not playing Do you think it's because I put it in sounds service?

vale mango
#

bro I never touched sound service in my life...

grave sparrow
#

sometimes wrong placed sounds don't play for some reason

vale mango
#

so this aint workin?

grave sparrow
#

oh wait

#

It works

vale mango
#

.

grave sparrow
#

but It caused a bit of lag

#

how do I go around this?

vale mango
#

too many sounds playin?

#

Bro......

grave sparrow
#

no

#

just one

#

let me try typing out new words

#

one sec

#

no way

#

my roblox studio just crashed crying

#

🙏

vale mango
#

...

grave sparrow
#

But I didnt wrap it in a for loop?

vale mango
#

did u play it once then?

grave sparrow
#

I ment while

grave sparrow
#

Maybe cause Im playing it when the player joins perhaps?

vale mango
#

so?

grave sparrow
#

only thing I added was this

#
local String, String2 = "hello", "Hi, my name is flower."

local TL = script.Parent.TextLabel
local Sound = SS:WaitForChild("Undertale - Flowey Talk (Normal)")

local new_thread = mod.new_threadObject(TL, String, 0.03, Sound, true)
local new_thread2 = mod.new_threadObject(TL, String, 0.03, Sound, true)

new_thread:play_thread()
new_thread2:play_thread()

vale mango
#

how long is the sound

#

also is it still laggin

grave sparrow
#

no nvm think that was just my wifi

vale mango
#

...........................................

grave sparrow
#

LOL

vale mango
#

alr slide 500 dollars

grave sparrow
#

tf

#

hell no

vale mango
#

tf u think this is

grave sparrow
#

bro Im BROKE

vocal tundraBOT
#

studio** You are now Level 12! **studio

grave sparrow
#

gotta feed my kids

#

🙏

vale mango
#

no you dont

grave sparrow
vale mango
#

let em starve

grave sparrow
#

NAH

#

alr well since this is fixed ima go fix this module

#

ty