Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I break a waiting script? Stopping wait(1) after being ran before completing task?

For 4 consecutive days, I've been looking for a solution to stop a script from performing its waiting task after the wait(1) has been ran. I want to stop the script from waiting in the middle whilst waiting.

The only conclusion I've come up with is to do script:Destroy() and then re-clone it but I rather avoid doing that.

Is there any way I can make this work?

while wait(5) do
	print("Script is waiting 5 seconds but I want it to break the while loop while it's waiting.")
end
--Outside function or method to break this while loop before wait() re-runs task? ^

2 answers

You could use break for stopping the loop, or if you want it to stop after a specific time, use for loops, just like this example:

for i = min, max do
-- function
end

Hope I helped.

6 comments

0

break happens after the wait task is performed. For loops run instantly and adding a wait(0.1) inside them and setting max to 10 is an inaccurate wait(1). Sorry, didn't answer my question.

0

I see what you want now, didnt read good. Sorry, I don't know anything that can work like that.

0

Actually surprisingly recreating a wait(1) using for loops actually worked.

0

then accept my answer if u can ;)

0

I'm not sure how to do that. LOL help, sorry.

0

I can't really accept your answer because well.. Even the solution that I created doesn't 'really' break a wait(). I got the idea to my solution thanks to you but however It doesn't really answer the question.

Found out the solution on my own.

ifStepped = true

function clock()
	spawn(function()
		
		for i = 1, 10 do
			i = i + 1
			wait(0.1)
			
			if i == 10 then 
				--[[If one second complete then we can re-run clock.
					can also use this and modify this if statement to see if the player stepped out of the region to stop the timer. This way when the player re-joins Region3 the timer / player points re-start exactly from 0.
				--]]
				print("broke")
				ifStepped = true
				break
			end
		end
	end)
end

spawn(function()
	while wait(0.1) do
		--This while loop checks if player is in a region3 (just an example)
		print("Ran")
		--Running the 1 second to see if player stepped out of region 
		if ifStepped == true then
			ifStepped = false
			--If this is true we run clock and we changed it to false not to run clock again.
			clock()
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.