RunService.Stepped loop

Hi! I really forgot if you could make loops with RunService.Stepped. Would you guys mind letting me know how to make it? I would appreciate some help, since While loops cause some lag inside my game and I would like to avoid them.

1 Like
local runservice = game:GetService("RunService")

runservice.Stepped:Connect(function()
--code
end)

Also, a tip, if possible try not make many scripts with loops, try make one script with one loop that connects to the other functions. E.G:

local runservice = game:GetService("RunService")

runservice.Stepped:Connect(function()
function1()
function2()
end)

As oppose to 2 scripts with a loop each.

3 Likes

Thanks man! That will work with my script.

1 Like

RunService.Heartbeat depends on the Client’s local machine’s performance so is probably better suited where you’re taking performance into consideration.

RunService.RenderStepped actually fires prior to frames being rendered regardless of completion of Physics simulation, and can cause performance issues.

local RunService = game:GetService("RunService")
 
local RATE_PER_SECOND = 2
 
RunService.RenderStepped:Connect(function(step)
	local increment = RATE_PER_SECOND * step
end)
sample from the developer hub
3 Likes

Thanks you too man! I will make sure to make the least scripts as possible.

image

5 Likes

If however it is necessary that 2 loops run at the same time, use threading to do so, but note that doing this frequently also causes performance issues.

Threading : Documentation - Roblox Creator Hub

2 Likes

I know that, i’m just saying, if possible to lower the amount of loops running.

2 Likes

The word “threading” brings using coroutines directly to mind:

local stepped = game:GetService('RunService').Stepped

local loop1()
    while true do
        -- ...
        stepped:Wait()
    end
end

local loop2()
    while true do
        -- ...
        stepped:Wait()
    end
end

local coro1 = coroutine.create(loop1)
local coro2 = coroutine.create(loop2)

coroutine.resume(coro1)
coroutine.resume(coro2)

spawn is all but deprecated by most of the developer community anyway.

I’m only storing these coroutines in a variable in case you want to manage them directly as they are running, but if you don’t need that you can just use wrap like so:

coroutine.wrap(loop1)()
coroutine.wrap(loop2)()
2 Likes

One thing to point out. This is an event and not a loop. Loops are different. One example, loops will yield the code unless you use run it on a new thread. Event connections don’t yield. You can also disconnect events very easily.

2 Likes

It is worth pointing out that connecting to Stepped directly is only technically a loop if your code doesn’t yield, in which case it is part of Roblox’s event loop. If it yields, then it will almost certainly* resume after the Stepped event is over.

*unless you :Wait() for an event that gets fired by another Stepped event handler after yours, which is an unlikely edge case that should not be relied on

You could perhaps :Wait() for Stepped for each iteration of your loop, if you yield in your loop but still need to align to Stepped for whatever reason. (Perhaps physics-based calculations? But those shouldn’t yield, so you should be good binding to Stepped directly.)

2 Likes

Does it work with mouse functions like said you click the part and move it around within the veiportframe?

example like the R in super nostalgia zone…

im struggling on this just to get it too work…