Is it possible to stop a wait(math.random()) loop immediately?
Because for whatever reason, even if I add checks if a certain value is false, it breaks the loop, but it continues to finish the loop first before actually breaking.
deleted code
Is it possible to stop a wait(math.random()) loop immediately?
Because for whatever reason, even if I add checks if a certain value is false, it breaks the loop, but it continues to finish the loop first before actually breaking.
deleted code
If you’re trying to understand how to break loops, such as pairs, or while true do’s or etc, I recommend reading this.
You can get into more detail with this when trying to break a loop.
break
Stops the loop. (must be inside a loop)
I’ve already read that a long time ago.
I know how to break loops as shown in the code.
if script:GetAttribute("Random") == false then break end
My problem is it not that it breaks, my problem is that it finishes the loop and THEN breaks it. It’s a break-delay until all children inside the parent are done running the loop. The delay until it break is like 2 seconds…
you can do a if statement
if value then
return
end
while not script:GetAttribute("Random") do
-- replace while true do with this
I have tried that method already.
The delay is still as follows… 2 seconds.
Making a GIF to show the error more specific…
Ohh, you’re trying to break a wait() statement aren’t you?
Break right after the wait. You can’t stop a thread from resuming in Roblox for some reason.
Exactly, since wait() statements are not able to be broken off you cannot break these types of parts in code. wait() statements have to go in first then it’ll break is the only plausible situation you can do.
b64 is Correct on this as it’s displayed above me.
Pay attention to the output in the GIF.
https://gyazo.com/aff37ea529f5f6eeefd1b3462bad0559
When it says “Stopping loop” it still trying to finish the loop and then break.
And that breaks after ~0.7 seconds approximately.
EDIT: I want it to be breaking instantly though, not delayed.
The best you can really do is put the if everywhere. I know this sounds tedious but according to the current replies it seems impossible
That also what I tried… Putting the check literally everywhere in the code of the loop.
Same thing applies as shown in the GIF i sent above.
If I understand what you want correctly, you just need to stop the spawned thread from running if the loop is broken. I’m pretty sure it’s as simple as adding another check after the wait()
local Fixture_Array = workspace.Technica.Fixtures:GetChildren()
if script:GetAttribute("Random_Changer") == 1 then
while true do
if script:GetAttribute("Random") == false then break end -- this is the check where i want it to stop the loop..
for i,v in pairs (Fixture_Array) do
spawn(function()
wait(math.random())
if script:GetAttribute("Random") == false then return end -- Add check here, use return instead because you're inside a different thread, not a loop
v.Head.Lens.Transparency = 0 v.Head.Beam.SpotLight.Brightness = 10 v.Head.Beam.light.Transparency = NumberSequence.new(0)
wait()
v.Head.Lens.Transparency = 1 v.Head.Beam.SpotLight.Brightness = 0 v.Head.Beam.light.Transparency = NumberSequence.new(1)
end)
end
wait(0.2)
end
end
The if statement below wait(math.random()) causes my script (loop) not to run at all when firing the function.
Removing that makes it run.
Here’s me spamming ALT to fire the function but nothing will happen according to the code you’ve sent.
Which loop do you want to stop? The while loop or the for loop?
The entire function…
But when breaking the entire function which is basically the while true loop, makes the break command delayed by almost a second instead of doing it instantly.
After several attempts of trying to add the check right below while true do and/or wait(math.random()), break does not kill it off immediately.
The function is stored inside a ModuleScript, as I’m trying to make a single-script-architecture so I can avoid using a seperate script for this function just to disable and enable a script.
The code you provided has a couple of bad practices that are leading to this issue. Assuming that the code you provided is in some kind of event connection, a much better way to do this would be:
local Fixture_Array = workspace.Technica.Fixtures:GetChildren()
if script:GetAttribute("Random_Changer") == 1 then
for _, fixture in pairs(Fixure_Array) do
task.spawn(function()
while true do
task.wait(math.random())
if script:GetAttribute("Random") == false then return end
// handle your light effect here
task.wait(math.random())
if script:GetAttribute("Random") == false then return end
// handle your light effect here
end
end)
end
end
The changes to note are:
Removal of the while true do
loop.
Before you were constantly spawning new threads for your light effect, which is a horrible practice since it’ll add another thing for the script to be running, slowing down the runtime.
Using the task library.
The task library function are pretty much the same, but they’re better for reasons that i won’t explain here. ( feel free to research this on your own )
Checking every time the light effect changes.
Using wait()
yields the thread you’re running your code in, and while true do
loops only check if the value is still true whenever it gets back to the top. The reason your lights were not turning off instantly was because of this. The script would check at the top, run the first light effect, get yielded, run the second light effect, get yielded, THEN checking again at the top. This causes the script to have to execute the entire cycle before getting checked.
Hope this helped! ^-^
( apologies if the formatting is poor, as i’m on my phone at the moment )