Game using up a lot of memory

I have a problem with my game using a lot of memory, I have a few

while true do

and BindToRenderStep, could that be an issue? what are some common causes of memory usage? The memory slowly rises constantly, even with me not doing anything.

maybe make it use while wait() do instead. This is to stop some scripts becoming exhausted and therefore reduces memory used

I have a task.wait(0.01) in each of the loops so wouldn’t that be the same thing just not as clean?

Never use the wait inside of while loops, it’s better to do this:

while true do
    task.wait()
end

:slightly_smiling_face:

1 Like

I only use wait() if I really need too. This is only so that I don’t exhaust some scripts ( I know that these task.wait() and wait() etc are great too)

I have a task.wait(0.01) in each of the loops

Can I ask what the issue is with doing

while task.wait() do

is?

I was doing it for a while until I’ve seen people discourage it, but I’ve never got down to the problem.

Because it does not respect script.disanled or script:destroy(), so they don’t feel safe with it

1 Like

Instead of using while true do
you should do something like

leaderstat.Money:GetPropertyChangedSignal("Value"):Connect(function()
-- code
end)

this is just an example, and it only runs when the value changes. It can be any property that you can detect. Using while task.wait() do or while true do is not a good practice and i don’t recommend doing so.

I’ll change as many while loops as I can to events.

1 Like

The main issue is that you’re creating a never-ending while loop, typically when creating loops of any kind (for, repeat, while) you should always have a breaking condition (a condition which when satisfied causes the loop to break/end). Since “wait()” and “task.wait()” and obviously “true” will always resolve to true (loops which use those expressions as their breaking conditions will never be terminated).

1 Like

So how do I make something run forever without using a while loop? Like a conveyor.

:BindToRenderStep(), .RenderStepped, .Heartbeat, .Stepped.

1 Like

What’s the difference? Don’t they run forever too?

No, since those connections are removed if the script is destroyed/disabled.

while true do loops will not recognise the disabling of a script.

1 Like

Oh so if a script is removed with a while true loop inside it will keep going even though it doesn’t exist anymore?

task.spawn(function()
	while task.wait() do
		print("hello")
	end
end)

task.wait(5)
script.Disabled = true
task.spawn(function()
	while task.wait() do
		print("hello")
	end
end)

task.wait(5)
script.Disabled = true
local Run = game:GetService("RunService")

Run.Stepped:Connect(function()
	print("Hello.")
end)

task.wait(5)
script.Disabled = true
local Run = game:GetService("RunService")

Run.Stepped:Connect(function()
	print("Hello.")
end)

task.wait(5)
script:Destroy()

With the first and second code snippets even when the script is disabled/destroyed the while loop still continues to execute. With the third and fourth code snippets when the script is disabled/destroyed the in this case “.Stepped” event is disconnected from its connected callback function.

2 Likes

Alright thanks for the advice! :heart: