More Efficient Scripting Tools?

Hi,
I’ve been trying to learn more methods to make scripting more efficient.
I want to learn about more things that I could use to make scripting easier and more efficient in general.
I’ve been trying to use things like Modulescripts and Metatables more (although I can’t really find much of a use for metatables - if you can point out why they’re so good that’d be helpful too.)
Sending me links to already existing articles is fine, the search function on the forum won’t work for me at all :slightly_frowning_face:
Thanks!

The absolute 100% guaranteed way to code more efficiently is to write more documentation. The most efficient documentation you can write is a description of what a function should do before you start writing it. Doing this makes your code much easier to test and verify as operating correctly, which in turn means you will have fewer bugs appearing at the same time as you add code. Avoiding multiple overlapping bugs is crucial to debugging quickly, overlapping bugs make issues almost impossible to diagnose.

3 Likes

Here’s a bunch of tips of mine:

  1. Use the task library for wait and delay (eg. task.wait(1))
  2. Use minimal amounts of server scripts, less = more performant
  3. Don’t do animations on the server, it gets laggy fast
  4. This may sound weird, but don’t over-document your code. Just add some simple comments, like --// Variables and --// Tables
  5. Always debug code before actually testing it out, put some prints in every new function you make so you don’t have to sit there banging your head against the wall for 20 hours figuring out what’s going on :skull:
  6. Try properly version-controlling scripts. For example, if you had a melee system, you should have a single localscript in serverstorage, and when the game loads, clone it to all tools. Now, you don’t have to edit every single identical script.
  7. Pre-create stuff like sounds and animations so you don’t waste precious lines of code to make them
  8. Make functions instead of repeating code
  9. :Disconnect() RBXConnections (eg. Part.Changed) when you don’t need them anymore

I also will often make tables for services instead of making, like, 3 variables for them. Example:

local services = {
    StarterGui = game:GetService('StarterGui')
}

I’m assuming you kinda meant efficient as in readable or future-proof, I aimed this more towards performance, but it should still make scripts more readable. Hope something here helps!

3 Likes

Metatables are a big thing in OOP
It allows to take less memory by “connecting” tables to another.

Here is an example.
Lets say i want to make my own object class which has 3 functions, Destroy, Move, ToggleAnchor,
Before my knowledge of metatables, i did this

local objClass = {}
function objClass.new(Position)
   local usable = true
   local anchored = false
   local obj = {
      Move = function(x) 
         if not usable then return end 
         Position = x 
      end,
      Destroy = function() usable = false end,
      ToggleAnchor = function()
         if not usable then return end
         anchored = not anchored
      end
   }
   return obj
end

Now, whats wrong with it?
If you create too much "objClass"es, You are creating a memory leak, and its not even a nessecary memory leak because you are creating the same data over and over!
The solution is to NOT create the same data over and over again. How? metatables have this covered for you

local objClass = {}
objClass.__index = objClass
function objClass:Move(position)
   if not self.usable then return end
   self.Position = position
end
function objClass:ToggleAnchor()
   if not self.usable then return end
   self.Anchored = not self.Anchored
end
function objClass:Destroy()
   self.usable = false
end
function objClass.new(Position)
   local class = {}
   setmetatable(class, objClass)
   class.Anchored = false
   class.Position = Position
   return class
end

Behind the scenes, Its “connecting” a table to the objClass so if you have like a millions of classes, its connected to this one objClass. And as you noticed, we didnt move variables like class.Anchored and thats because they can be different depending

I know i have wrote a bit of an essay till the point its slightly off topic but the google search bar has the things you need to find out what these do/ are useful for

Module scripts play a big role in developing too but im not gonna write another essay but you can go to the devhub to find out

1 Like

I did mean more performant. I’m horrible with performance stuff.
I do use things like task, version-controlling, clientsided animations (although I don’t use animations very much), using functions, etc…
I didn’t know that using more server scripts is a bad practice, I’ve been trying to combine them into fewer scripts and also make things more client-sided when possible, along with using modules to help a lot with organization.
And oh my gosh. I remember learning about :Disconnect() a while ago but I completely forgot about it. Thank you so much for reminding me of this, I definitely need to use it much more than I do.
I never thought about placing services in tables, that’s actually very smart… thanks for that.

1 Like

There’s nothing to it. You just gotta find interest in the thing that you are trying to achieve.

1 Like

I’m able to achieve what I’m trying to do.
But I want to learn to do these things better.
I’ve heard things like this on posts a lot, and yes it’s true, but what I’m specifically asking for is just, tools and methods for things that could help make it easier, more efficient, etc.