How can i do this in a module script and not in a normal script

I’m not really sure what to call this so i don’t even really know what to look up
basically i saw this in a script as I was trying to learn oop, and I didn’t know you could do self.test or whatever variable to make something, im not even sure what it really does. I tried something similar in a script and I wasn’t able to use the same or similar notation. What exactly is happening here?

local part = menu:Test()
--module script
function module:Test() 
	self.test ="Part",workspace)

: is only used during OOP. There are plenty of articles on how to use metatables in modulescripts!

If you want to use modulescripts without metatables, change : to . and it will work.

-- Script
local moduleScript = require(<modulescript>)
local part = moduleScript.Test()
-- Modulescript
local module = {}

function module.Test()
    local part ="Part")
    ... -- Whatever code you wish to run regarding the part
    return part

return module

The “template” for OOP looks like this:

local module = {}
module.__index = module

    local self = setmetatable({}, module)
    ... -- Other init code
    return self

function module:Foo()
    -- Self is passed through implicitly

return module

oh i got it to work sorry i didn’t explain it good, the problem im having or rather that im confused is the implementation of self.variable
below is the code in question

    local self = setmetatable({} , Zombie)  
    self.template = zombieModel:Clone() 
    self.template.Parent = workspace 

    return self ; 

function Zombie:Print(s)
    print("Zombie says "..s) 

function Zombie:Die()

im wondering why self.template is a valid variable name or how.

local item = {}
item.__index = item

	local data = setmetatable({}, item) --this just makes it so that if the thing you're trying to access doesn't appear in data, it will search 'item' for it
	item.color = color
	return data

function item.ChangeColor(passedItem, color)
	passedItem.color = color

So in this example, you could call item.ChangeColor()

local myItem =,,1,1))

Basically you have to pass in myItem so that the code can actually adjust the data for that object. Otherwise it would be confused because color isn’t in that scope.

It’s annoying to have to say myItem every time though. You are already using the myItem variable in that line so it feels a bit odd to be passing it in as well.

So without changing any of the above code, the colon will automatically pass the item you called it on as the first argument. So these 2 things do the same

myItem.ChangeColor(myItem,,0,0)) --You have to pass myItem in
myItem:ChangeColor(,0,0) --The colon automatically passes the myItem in as the first item

Now this works when you make the function using ‘.’ syntax.

function item.ChangeColor(passedItem, color)
	passedItem.color = color

But what if you use colon syntax for making the function?

function item:ChangeColor(color)
	self.color = color

With colon syntax you notice I removed the passedItem parameter. That’s because when making a function with colon it secretly adds a parameter which it calls self. You can’t see it, but it’s there right before the color parameter.

To see it a bit more clearly you can call it 2 ways again
The colon method


Or, you could call it with dot method

myItem.ChangeColor(myItem,,0,0)) --You have to pass myItem in

Colon automtically added the self variable, but the dot method you would again have to pass in myItem which ends up being the self variable.

1 Like

Oh, right. self is a table, and as you know you can have a table and create new keys on it.

If you have this code:

local a = {}
a.Key1 = 1
a.Number = "This is a value!"
a.Planes = 42


The output would be:

    Key1 = 1,
    Number = "This is a value!",
    Planes = 42

In the same way you are adding a key template to the table, with the value being that zombie model clone.

Also, you don’t need ; after your return statement.

1 Like

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