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:
print("Starting script")
game.Workspace:WaitForChild("Humanoid",0.001)
print("found character")
local player = game.Players.LocalPlayer
local character = game.Players.LocalPlayer.Character
local TextLabel = script.Parent
local stop = game.Workspace.Stop
local start = game.Workspace.Start
local screengui = player.PlayerGui.ScreenGui
start.ClickDetector.MouseClick:Connect(function(click1)
if click1 then
print("clicked green button")
for i = 1,math.huge,1 do
wait(1)
TextLabel.Text = i
end
end
end)
stop.ClickDetector.MouseClick:Connect(function(click2)
if click2 then
print("clicked red button")
-- what do I add here to make the for loop stop?
end
end)

2 comments
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.
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.