How to add a wait to RunService

I want to add a wait(1) in a forloop that is in RunService how do I do this because

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
	for Number, Instance2 in pairs(workspace:GetChildren()) do
		print(Number, Instance2)
		wait(1)
	end
end)

does not work.

1 Like

Put the wait(1) outside the loop if you want it to get all the children of the workspace every second

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
	for Number, Instance2 in pairs(workspace:GetChildren()) do
		print(Number, Instance2)
	end
	wait(1)
end)

You may have to use a debounce similar to this

local RunService = game:GetService("RunService")

local deb = false

RunService.Heartbeat:Connect(function()
	if deb then return end
	deb = true
	for Number, Instance2 in pairs(workspace:GetChildren()) do
		print(Number, Instance2)
		wait(1)
	end
	deb = false
end)
3 Likes

If you want to get them every second why not do:

while true do
    for Number, Instance2 in pairs(workspace:GetChildren()) do
		print(Number, Instance2)
		wait(1)
	end
end

You don’t need RunService to do this.

It’s better to use RunService whenever possible

False. Run service should be used when you’re doing something every frame, not because you want to have a wait repeating every frame. This is horrible practice.

There are a lot of things that could be fixed by using a loop instead, for example, every frame you’re checking for a debounce, while you shouldn’t have to in a loop. There are going to be hundreds if not thousands of frames that connection will just check the debounce for absolutely no apparent reason.

This is just going to take up performance(although very little) which is not necessary for this simple of a task.

9 Likes

I guess adding on to the above post

I suppose that RunService’s Events should be used only as an alternate for a faster wait(), but not using it every time in every loop you may come across (Which wouldn’t make sense anyways)

Things like a longer wait delay (wait(3)) I think would be fine but it wouldn’t be exact

You could also break the while true do loop if the thing you’re looking for inside the pairs loop is valid

(Not 100% certain on this)

1 Like

This is not entirely true, the benefit of using the run service is that you would not need to create a new thread if there are instructions in the script after the while loop. Also, it is not terrible practice nor it is any more performance intensive, if you test the script performance of using the run service it is going to be 0% for the frames when the debounce is active.

You’re creating around 60 of the same things each second, which is really unnecessary. As I’ve stated above:

Creating 1 thread is still better than running something every frame(for thousands of frames) without doing anything. Would you just leave an empty RunService connection in your code? Probably not. but the thing is, that’s what this code is basically doing:

RunService.Heartbeat:Connect(function()
	if deb then return end
	deb = true
	for Number, Instance2 in pairs(workspace:GetChildren()) do
		print(Number, Instance2)
		wait(1)
	end
	deb = false
end)
1 Like

There is no performance impact of running a if statement every frame? I don’t understand your argument or your logic. You are saying that one approach is better than another without any factual evidence. Also, creating a new thread will destroy the stack trace for errors unless your using a custom solution.

I just tested both methods in studio, and they script performance of both spiked when the print statements were being executed and were 0% while the wait was executing, I don’t really get your argument. I agree the person should be using a while loop here, but there is no “performance” impact.

Even though it’s not much of a difference, running if statements is always worse than running none.

This wasn’t something about performance, more practice-wise.

1 Like

There is no difference, your argument is based on a illogical assumption, lol you are stating random things without anything evidence.

“This is just going to take up performance(although very little) which is not necessary for this simple of a task.” - This statement is incorrect, as stated I already tested it and there is no difference in performance.

“False. Run service should be used when you’re doing something every frame, not because you want to have a wait repeating every frame. This is horrible practice.” - This statement also makes zero sense. If you hook something up with a connection on a run service event, it can easily be cancelled in the future by simply disconnecting it, and no you don’t only use run service events on things that must run every frame. Sometimes you might want something to run every x seconds but it still needs to be done at a specific time, whether on RenderStepped, Stepped or heartbeat, in such cases a while loop would not suffice.

I have a question, Doesn’t the “if deb then return end” completely ends the function? I’m confused.

no, basically if deb == true then return end, this would end the function if it was debounce, if debounce was false, it would jump over that and run the rest of the script

1 Like