Loops executing function incorrect amount of times

you’ll need the Heartbeat event and/or Stepped event for the server
and then you can use the RenderStepped event for the client.

In the for loop, what would happen if you moved it out of the spawn(function() thread?

if he removed the spawn() it’ll just yield the whole script until the loop is finished. (which isn’t what you want in most cases)

It would mess with the functions that run while the gamemode is active just like with the timer.

My idea:
checkStatus() returns true. that then becomes a while true do which will run forever.

If you wanted to do it multiple times, do:

spawn(function()
  local status = checkStatus()
  while status do
    -- Everything here
    status = checkStatus() -- Refresh status value
  end
end)

This might be entirely wrong, but worth a shot.

I am aware of that, I just want to see if anything at all would change, because I don’t know why the for loop loops for an incorrect amount of times.

How could I use Stepped while making it delay 1 second?

Please share more of your code with us, the issue is clearly not with the loop as it’s not reproducable by anyone else. Your logic elsewhere must be incorrect. Please also post code like so instead of screenshots:
```
code here
```
and this will produce:

code here

is there any wait() commands within CheckStatus()?

I edited it because I found that the issue was with wait and not the loop.

There are not.

This is the CheckStatus() function:

function checkStatus()
	if not Stats.Active.Value then return false end
	if defenderScore.Value >= goal.Value then return false end
	if raiderScore.Value >= goal.Value then return false end
	if timeRemaining.Value <= 0 then return false end
	return true
end

This will not produce what you have shown, there must be something you have done somewhere else that is creating this thread multiple times. As I said, please share the complete version of your code that contains this loop and we will be able to help more.

The issue where your loop is “repeating more times than expected” won’t have anything to do with the wait.

It produces what I showed. I don’t know why and it shouldn’t that’s why I posted this. The only reliable way to make the loop execute the correct amount of times is to remove the wait.

but his first loop is within a spawn() nothing else other then the “CheckStatus()” function can effect it. (and its not effecting it)

idk why his loop is going so fast considering the wait() will work fine. (even if ‘wait(1)’ only yields for 0.6 seconds instead of 1)

you also have to understand that roblox doesn’t actually let you run code concurrently, so the more things you have executing at once the more your time will be off by. (even if the code is in a different coroutine, someone correct me if im wrong though)

give this a read for more in depth information @ArtillarySquad

But, that code on its own will produce the correct result:
image
There must be a logical issue elsewhere in your code, which is why I ask you to supply it.

Evidently, he has not shared the complete code - something else has to be affecting it.

that isn’t the code he is using though, he is using the while loop to print the number value, so I don’t understand what you mean tbh.

@ArtillarySquad is your spawn() being created multiple times? this will run multiple loops back to back. (which means it’ll be printing to output too fast)

1 Like

It is code he has replaced his code with to test, if you look at the post. This makes it evident that something else in code (that hasn’t been shared) is causing this issue.

EDIT: You even asked it yourself implicitly, asking if the rest of the code could possibly be creating this thread multiple times.

I have found the issue. It was the function that contained the loop being called multiple times.

1 Like

That’s great to hear! In the future, if you test certain parts of your code on their own to see how they behave on their own, it’ll be easier to identify if the problem is actually with that part of the code or elsewhere. In this case, you would have come to a solution quicker if the function you speak of was shared.

Always good to figure things out for yourself though, and satisfying too!

The issue wouldn’t have arisen in the finished product. The initialize function is going to be executed by a chat command, but I temporarily made it activate when a start brick was touched because the chatted event isn’t reliable in studio. I didn’t do a debounce for when the start brick was touched.

2 Likes