Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to stop or start loops remotely?

Asked by 4 years ago
Edited 4 years ago

Hello, I'm trying to make some code that will run a for loop to count up from 1 to infinity when you click a green button and stop the for loop when the red part is clicked and I'm not sure what to do. I'm asking if there's any way to stop or start a loop remotely? I'm also asked if there's a way to not run the code if I clicked the button more than once because if I click the button more than once, the code runs again and displays numbers all over.

Here's the code:

01print("Starting script")
02game.Workspace:WaitForChild("Humanoid",0.001)
03print("found character")
04 
05local player = game.Players.LocalPlayer
06local character = game.Players.LocalPlayer.Character
07local TextLabel = script.Parent
08local stop = game.Workspace.Stop
09local start = game.Workspace.Start
10local screengui = player.PlayerGui.ScreenGui
11 
12start.ClickDetector.MouseClick:Connect(function(click1)
13    if click1 then
14        print("clicked green button")
15 
View all 27 lines...

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

We're gonna be using the 'break' function that stops/breaks loops.

You can have a variable determine whether or not the loop should continue running

Since you're using a for loop, you can just add an if statement for the variable, and if that variable is true/false, then break the loop

Example:

01local runLoop = true
02 
03for i=1,math.huge,1 do
04    if runLoop == false then
05        break
06    end
07    wait(1)
08end
09 
10stop.ClickDetector.MouseClick:Connect(function()
11    runLoop = false
12end)

As for stopping something from running multiple times, we're gonna use a debounce

Basically just a variable that determines if the thing is already running or not, if it is already running, then it won't run it again.

Example:

01local canRun = true
02 
03start.ClickDetector.MouseClick:Connect(function()
04    if canRun == true then
05        canRun = false
06        --run your stuff
07    end
08end)
09 
10stop.ClickDetector.MouseClick:Connect(function()
11    canRun = true
12end)

ez

0
Is there a way to run the code again when I already activated it? If I press the green button after I pressed it to start, nothing changed. I want to continuously press the start and stop buttons and for the code to work. Green_Lime2 80 — 4y
0
First of all, I've mixed up the true/false in my example, so i edited it, second of all, I've edited it again to add the part that allows it to run again after pressing start, my bad. Eagle0022 75 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I asked in the comments what I needed help with next so read that if you want.

Here was the code that I used with your help:

01print("Starting script")
02game.Workspace:WaitForChild("Humanoid",0.001)
03print("found character")
04 
05local player = game.Players.LocalPlayer
06local character = game.Players.LocalPlayer.Character
07local TextLabel = script.Parent
08local stop = game.Workspace.Stop
09local start = game.Workspace.Start
10local canRun = false
11local runLoop = true
12 
13start.ClickDetector.MouseClick:Connect(function(click1)
14    if canRun == false then
15        canRun = true
View all 32 lines...

Answer this question