Is this possible?

local event = boolean that returns true if a certain thing happens
for i = 1, 10, 1 do
      wait(1) -- i want this for loop to break ANYTIME "event" becomes true
end

so we have a forloop that waits 1 second ten times,
i want the loop to break when “event” happens, therefore
the wait function needs to cancel aswell

is there a way?

Yea just give it a value

local event = boolean that returns true if a certain thing happens
for i = 1, 10, 1 do
if not event then
      wait(1) -- i want this for loop to break ANYTIME "event" becomes true
event == true
end
end

idk smth like that

Event = true

for i = 1, 10 do
  -- break once Event becomes false
  if not Event then break end
  print("Loop", i)
  wait(1)
end

The main problem is that you are not checking for the event on each loop iteration. Because of this, the script will wait the full 10 seconds regardless. You need to check for the event inside the loop.
This is known as polling. The there are two proper ways of doing this.

Method 1:

local event = boolean that returns true if a certain thing happens
if event == false then
	for i = 1, 10, 1 do
		task.wait(1)
		event = boolean that returns true if a certain thing happens
		if event == true then
			break
		end
	end
end

This method is basically the same as yours, except for the following items:

  • wait() has been replaced with task.wait() which is superior, fast, and part of the task library.
  • It checks to see if the event has occurred before it enters the loop. What’s the point of going into the loop if the event has occurred? That just introduces an unnecessary delay.
  • It checks for the event inside the loop after the delay. It should be after the task.wait() because if it is before, then it checks it twice in rapid succession and wastes CPU time.
  • After checking for the event and storing the result, it checks the result. If the event did in fact happen, then it breaks out of the loop.

Method 2:

local event = boolean that returns true if a certain thing happens
local count = 0
while event == false and count < 10 do
	task.wait(1)
	event = boolean that returns true if a certain thing happens
	count += 1
end

The big difference here is the while loop. The count variable is used which performs the same function as i in method 1. The predicate of the while loop checks both conditions which are as follows:

  1. event is false which means that the event has not happened yet.
  2. That count < 10.

When either of these conditions become false, due to the and keyword, the entire predicate returns false and execution exits the loop. This has the advantage of not requiring an encompassing if statement because if event is true, it will skip the loop.

Personally, I would go with method 2.

EDIT: Instead of waiting a full second, there’s a way where you can wait only as long as needed. I’ll modify method 2 to show you.

local event = boolean that returns true if a certain thing happens
local count = 0
while event == false and count < 10 do
	count += task.wait(0)
	event = boolean that returns true if a certain thing happens
end

Instead of adding 1 to count every second, we can add the actual time that task.wait() returns, which will be on average 1/60 seconds (~0.0167 seconds). The loop will iterate every heartbeat of the engine. You can replace 0 with any time that you want. I use 0.1 seconds for a lot of things, for instance:

local event = boolean that returns true if a certain thing happens
local count = 0
while event == false and count < 10 do
	count += task.wait(0.1)
	event = boolean that returns true if a certain thing happens
end

The reasoning behind this is to reduce the amount of waiting that is required before execution can proceed. The shorter the wait times, the faster the game logic will proceed.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.