Efficient way of executing code?

Hey guys!! Working on a little project and I’d like some feedback on whether this code is formatted as efficiently as it can be. Essentially, im declaring all my functions, sounds etc. at the start and then once my clientevent is activated it runs everything. Keep in mind the function of this is to load up the first part of my game :slight_smile:

TLDR: Is declaring everything then putting into one function triggered upon event efficient??

Declarations:

Where code is ran:

sadly i am not experienced enough to answer your main question but i can tell you what i recommend

use :Wait() instead of :wait(), i might be wrong but i heard :wait() is deprecated.

do workspace instead of game.Workspace, it is shorter and references the same thing.

do Instance.new(class) instead of Instance.new(class, parent) and set parent property last. this devforum announcement explains why:
PSA: Don’t use Instance.new() with parent argument - Updates / Announcements - DevForum | Roblox

task.wait() should be used instead of wait(), task.delay instead of delay etc.
task library is more consistent and accurate and is meant to replace their soon-to-be deprecated counterparts. (they didn’t just update the function and instead made a new library because it may or may not break old games and make people sad abt it)

2 Likes

This is extremely helpful thank you!

:Wait()?
When I use it in game it shows an orange bar under it.
(im kinda new to lua)

:Wait() as in

RBXScriptSignal:Wait() instead of RBXScriptSignal:wait()

line 15 op wrote player.CharacterAdded:wait()

i was telling him to replace with player.CharacterAdded:Wait()

1 Like

In the OnClientEvent function there are some not exactly necessary Instance.new() calls.
Instead of creating the instances inside the client event function, you could improve efficiency by creating the instances along with the rest of your declarations, this way you can reuse the instances instead of creating new ones every time the function runs.

1 Like

To answer this, yes. Other than that, why don’t you make the GUI outside the OnClientEvent? It’s slower as it has to make GUI every time the remote is fired, not to mention if the remote gets fired multiple times there will be an overlap.