Script Optimizations

Just learn bytecode LOL
See in dissasembler what code gets compiled into

1 Like

Them spaced lines would mess me up too. Try to keep the all related chucks within a screens view as best you can.

I mean like how would you organize the folders and modules and rigs and stuff?

i like to have a utility folder in both replicatedstorage and serverscriptservice for client/server only scripts

It’s optional, but you can also have a folder for data modules exclusively, but if the data module is only used in a specific script (e.g. a datastore template) just parent it under the script in question

for assets, an asset folder in both ReplicatedStorage and ServerStorage, since the client will not need to access every asset

then you can have subfolders for models, characters and stuff, pretty much preference

P.S. For code snippets, paste them into in a temporary script and auto format them or something, 99% chance you’ll be ignored if you send a code block and it has a ton of spaces at the start

about the code, your spacing is wildly inconsistent, and even if it is for readability i dont see the point of spacing ends, that just makes it harder to read

from my understanding, a lot of things happen only if touch is enabled, so you’re better off
A. using a combined if statement
B. changing the TouchEnabled check to the outermost condition (this may not be ideal if touch somehow gets disabled)

this probably doesn’t matter but i’d change button text and things before making it visible

if abilityFound:FindFirstChild("Charge") then
	button.Charge.Visible = true
else
	button.Charge.Visible = false
end

you can improve a lot of your code that looks like this

button.Charge.Visible = abilityFound:FindFirstChild("Charge")

it’s hard to get into, but try to think of conditions more as booleans and you’ll cut down on unnecessary if-statements

I like to use a top-down approach. Define everything I can up top, helper functions next, work functions after that, and then the main loop. Always the same.. always know where to look for problems.

May I have an image on an example hierarchy for folders?

Another thing, this snippet of code I made roughly months ago so I decided to try to optimize it now.

I suggest you not to even try, whatever he coded there is a complete abomination and horrible piece of code, that’s all from me, thank you

Hater

2 Likes

Very good organization :clap: This just proves that you are a good scripter.

2 Likes
  1. Loops
    It is really important to not use pairs AT all now, and ipairs when not needed, they are both old, you should switch to either generalized iterator, or next variant.
for Key, Value in myTable do --// Gen iteration
for Key, Value in next, myTable --// next variant, also faster than Gen iteration
  1. Less polling and depend on Event Driven structure as much as you can
RunService.Heartbeat:Connect(function()
   if player.Health <= 0 then
      killPlayer(player)
   end
end)

--// Above is a bad, useless polling, consider switching to Events like .HealthChanged
--// so your code only runs when something changes
  1. Memory Management and Object Pooling
    In a horror game, you might be spawning effects, blood, or projectiles. Creating (Instance.new) and destroying (:Destroy()) parts repeatedly causes lag spikes.
  • The Solution: Object Pooling. Create 50 blood parts at the start of the game and hide them. When needed, move them to the correct position. When done, hide them again. Do not destroy them.

Now, since Yarik is not here, I will be the first one to mention that you can switch to ECS, since I assume you will have monsters, and other entities, but of course, your game might not require ECS at all, but you can begin switching to ECS once your OOP is starting to get messy

Also understand, that thousands of lines does NOT mean a bad game, a big game can easily have thousands of lines, tens of modules.

1 Like

fact check this @yarik_superpro

1 Like

Yarik is my student, he does NOT fact check me buddy
It’s even crazy that you need someone else to fact check the basics of game optimization, wild.

im too lazy to check the bytecode ngl

i’m glad someone mentioned this

1 Like

??? all 3 methods I’ve summarized are common sense, how are you going to benchmark object pooling by direct bytecode, bytecode does not have Instance.new nor :Destroy(), even if you were to replicate the class, you cant replicate what the engine does when these functions get called. The only way to benchmark this is within studio.

1 Like

I’d probably set it up something like this:

Summary

Workspace

  • Map (Folder) (very important if you don’t have multiple maps and instead have a world map, this will contain folders for sub areas such as islands)

ReplicatedStorage

  • Assets (Folder)
    • VFX (Only client really needs access)
  • Utility (Folder)
  • Data (Folder)
    • CharacterInfo (Data ModuleScript)

ServerScriptService

  • Scripts (Folder)
    • Data (Script)
      • Template (Data ModuleScript)
  • Utility (Folder)

ServerStorage

  • Assets (Folder)
    • Models (a majority of models will be accessed from the server)
    • Maps (Round-based game)

Template is a theoretical data module that only the data script would need access to. Ideally, you should just put it in the script itself, but it’s mainly preference.

The next variant being “faster” is one of those myths that has survived purely through word-of-mouth and cargo-cult programming. At optimization level 2, pairs literally compiles down into the same sequence. You’re not gaining cycles - you’re gaining placebo.

Generalized iteration is the only pathway endorsed by the Ministry of Performance.
Everything else is legacy coping mechanisms.

As for the ECS part:

The New Luau Order has reviewed your statement and determined that encouraging ECS “just because OOP might get messy someday” is a Level-4 Abstraction Hazard.
ECS is a tool, not a prophecy. Use it when needed - not as a ritual.

This reply has been officially approved by the Command Council.

3 Likes

i was wondering why it took you long to reply you were spending hours on that logo

what the heck am i reading right now

1 Like

You are witnessing the next evolutionary stage of Luau development.
To proceed, please surrender all unnecessary abstractions and report to your nearest Optimization Center for cognitive recalibration.
This is completely normal.

3 Likes