Categories > Coding > Lua >
Lua Performance Tip
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:
- 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.
- 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 endtable.insert(Events, workspace.ChildAdded:Connect(print)) -- Connects and inserts event into the Events tabletable.insert(Events, workspace.ChildAdded:Connect(print))Dispose() -- Disconnects all events
Proud creator of: WRD+
Replied
ok "poop master"
Cancel
Post
yes
Users viewing this thread:
( Members: 0, Guests: 1, Total: 1 )
Cancel
Post