How I Make Objects In Roblox Lua

All concepts talked about in this post are based on my intrepretations and personal experience, take all this information with a grain of salt

Firstly, i’m going to start of by saying that i’ll be going over a bit of an advanced topic so if you don’t know at least the basics of Lua, you should leave now (but who am i to tell you what you can and can’t do). Most of the advanced concepts in this post i’ll just quickly go over and provide links at the end of the post to resources that helped me learn this. This blog post will just show you how i use this in my current/future games.

Metatables

I use metatables to use/create my objects with a ModuleScript. Here is a conversation between a metatable and a table being indexed:
Table: Hey so the user wants the value of this key but i don’t happen to have it, do you?
Metatable: Let me check… Oh yup! Let me give you the value
Table: oh cool thanks! I was about to return nil.. heh
*akward laugh*

Code And Showacase

So far i’ve gone briefly over the building block of what i use to create objects, now it’s time for some juicy code. This here is an example of an empty Modulescript already set-up to have objects created off of it:

To quickly go over the code line-per-line:
1: Creates the table
2: Checks metatable when the table being indexed doesn’t have the value that it is being indexed with.
3: It’s a comment, read it.
4: A method that creates a new object
5: Creates a metatable using the table “module” and an empty table, read line 2
7: Returns the table to the script that requires the module.

Here is an example of a module script with Properties set:

Now the rest of the module (skipping one function as that contains private data):

Then in a script on the server, i require the module like so:

Then i set the discord property to another modulescript object (which is where the discord post is made) like so:

Afterwards, when a player joins i send the players id to the modulescript so that it can know if the player is in the watchlist:

Showcase

I use this same method of object creation to create “Pets” seen in this gif:

Image from Gyazo

Also note i was going to give this pet module as an example but using this watchlist was a much easier and shorter explanation of how the object creation works. Of course, using it in a simple watchlist is pretty redundant as it could have just been a simple function rather than a whole object but still, the point was to get the idea across.

Helpful Links:

Start here and work your way to the right: https://www.lua.org/pil/13.html
Info about .__index: https://www.lua.org/pil/13.4.1.html

Published by mawesome4ever

I am a scripter. I enjoy scripting things in Lua and enjoy teaching it as well as helping other with the language.

Leave a comment

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: