Categories > Coding > Lua >

Lua Performance Tip

PoopMaster

SoundInfinity

noticed

Posts: 120

Threads: 23

Joined: Jul, 2019

Reputation: 9

Posted

Yeah, you all might not give it much importance, but optimization in a script is really important, for your user and the performance of your script. Here are some things, that I suggest for you all to check when writing your scripts:

  1. Do I need to use a loop?
    • Most of the time a loop is unnecessary, for example, many may loop through the players to update information about them. An alternative is to use events, like: "PlayerAdded" and "PlayerRemoving" and have an update function instead, that way the client doesn't have to fetch the instances every millisecond.
  2.  Do I need to dispose (disconnect) events?
    • Yes, the more event listeners there are the more RAM is used, and as a result the script is gonna use more resources. So, how do you disconnect events? well simply store the event in a variable or table and invoke the "Disconnect" method on the event instance. Example:
local Events= {}
function Dispose() for _, event in next, Events do event:Disconnect() end end
table.insert(Events, workspace.ChildAdded:Connect(print)) -- Connects and inserts event into the Events table
table.insert(Events, workspace.ChildAdded:Connect(print))
Dispose() -- Disconnects all events
  • 0

Proud creator of: WRD+

jex

yes

Posts: 1799

Threads: 110

Joined: Nov, 2019

Reputation: 13

Replied

ok "poop master"

  • 0

yes

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )