How to make a game lag less

  1. A connection is when you do this:
    SomeService.Signal:Connect(function). If you leave unnecessary connections open it is considered a memory leak. Example of disconnecting a connection:
local playerAddedConnection
playerAddedConnection = players.PlayerAdded:Connect(function()
    -- Some code
end)

playerAddedConnection:Disconnect()

When you disconnect a connection, it will not longer fire (if that wasn’t obvious already)
Once is like Connect but it disconnects immediately after firing do you don’t have to do all the goofy variable stuff.

  1. The task library is a relatively new library implemented by Roblox that serves as a better replacement of wait(), delay(), spawn(), and more.
    For example, you will almost always want to replace wait() with task.wait().
    And for small performance improvements, you can use task.defer() instead of task.spawn() when you don’t care about the coroutine resuming immediately.

  2. RunService connections would look like this:

local runService = game:GetService("RunService")

runService.Heartbeat:Connect(function()
    -- Code here
end)

These connections fire every frame which makes them a viable replacement for while true do imo.
There is also RunService.RenderStepped and RunService.Stepped. There are differences to these signals which I would take a look at in the RunService documentation.
For better performance, use .Heartbeat whenever possible.

1 Like