Object Oriented Programming

Learn how to create your own object in Roblox!

by malakopter

Author Avatar

Introduction

Hello! Welcome to my tutorial on Object Oriented Programming in Lua. I'm ILoveMyJelly, and by the end of this you will know how to create objects via script and use them in YOUR game!

Setting up

There is a not a lot of setup, actually. Just create a ModuleScript in ReplicatedStorage and name it 'Sword', and a Script in ServerScriptService - Doesn't matter what you call this. By the name of the ModuleScript you can tell we're going to make a Sword! Not a real sword that functions, it's just an example.

Coding

Clear both scripts!

We're gonna start off with making the Sword object. Since it's a ModuleScript, we're gonna need to create a table and return it, like so.

local Sword = {}

return Sword

When creating objects in Lua, we want the object to reference itself, not the Roblox ModuleScript, so what we do is use metatables and __index. __index is known as a 'metamethod', a method inside a metatable and it gets called whenever someone indexes the table. For example, let's say we had a table known as Test, it was empty, and it had a __index functions that returns "Hello!", if someone did Test[234], even though it has nothing in it, it will return "Hello!", because of that __index function. __index doesn't have to be a function, it can be anything! It will just return the whatever the index metamethod is.

To make a metatable, we use the function setmetatable, the first parameter is a table we're turning into a metatable, and the second is the metatable. We're going to make the Sword object a metatable, and the __index will be the metamethod.

Going back to what I said when we want the object to reference itself, using what I just said, we can make it do that! Like so.

local Sword = {}
Sword.__index = Sword

return Sword

That may look silly, but when we're creating our object and making our metatable, when someone tries to calla function, it will go back to the __index and find it needs to index the Sword table, so it does and find the function, then calls it!

Just like every other object in Roblox (Vector3,Instance,BrickColor, etc), it's going to have a 'new' function (Like: Vector3.new()) So that's exactly what we're gonna do! We're going to add this into our ModuleScript:

function Sword.new()

end

Now we're going to make the metatable!

function Sword.new()
    return setmetatable({},Sword)
end

Wow! Now we have our metatable! To differ between different swords, we want an ID property for the sword. To do this, we're going to put it into the table we're turning into a metatable.

function Sword.new()
    local Id = math.random(-1000,1000)
    print("Creating sword with ID " .. Id)
    return setmetatable({ID = Id}, Sword)
end

I added a print to let me know when it's done, and the ID. Now we're ready to make our functions for the sword! Make this function UNDER the .new() function.

function Sword:Unsheath()
    print(self.ID .. " was unsheathed")
end

Notice the "self.ID"? That's referencing the ID property we created, when we call our functions from the sword, we will use : instead of '.' when calling functions, this is because ':' passes in the table automatically, so we can reference from inside our functions. Let's make a few more functions!

function Sword:Sheath()
    print(self.ID .. " was sheathed")
end

function Sword:Swing()
    print(self.ID .. " was swung!")
end

That's it for our Sword module, let's see what it is now.

local Sword = {}
Sword.__index = Sword

function Sword.new()
    local Id = math.random(-1000,1000)
    print("Created sword with ID " .. Id)
    return setmetatable({ID = Id}, Sword)
end

function Sword:Unsheath()
    print(self.ID .. " was unsheathed")
end

function Sword:Sheath()
    print(self.ID .. " was sheathed")
end

function Sword:Swing()
    print(self.ID .. " was swung!")
end

return Sword

Congratulations! You now have made your OWN object in Lua!

The Script will be very easy to do.

We know that our object is ReplicatedStorage, so let's get that. We're editing the Script now, not Sword.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

Like that. If do not know what GetService does, is it returns the actual Service of what you input, because you can rename services. To get the table that our ModuleScript returns, we will use the function 'require', this is how you use it.

local Sword = require(ReplicatedStorage.Sword)

Now we have our sword module, we can create our objec by using the .new() function.

local MySword = Sword.new()

That's your object! Now we can use all the functions we want!

MySword:Swing()
MySword:Sheath()
MySword:Unsheath()

And our total Script is:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Sword = require(ReplicatedStorage.Sword)
local MySword = Sword.new()
MySword:Swing()
MySword:Sheath()
MySword:Unsheath()

Conclusion

OOP: Object Oriented Programming. We learnt about metamethods, creating metatables, ModuleScripts, requiring MoudleScripts, another way of getting services, and most importantly, OOP! Here are some challenges I'm giving to you regarding OOP.

Thank you for reading this, and I hope you know now!

View in-game to comment, award, and more!