Hello,
I have a problem with the loop. I want to create a loop that will count to 100, but when I click a button I want to restart it and count to 100 again. How can I change the value form “i” to 0 to loop back to 100. Only my gui switches to 0. Script:
local gu = game.Workspace.w.SurfaceGui.TextLabel
local click = game.Workspace.c.ClickDetector
local de = false
click.MouseClick:Connect(function()
de = true
end)
for i = gu.Text, 99 , 1 do
wait(1)
gu.Text += 1
if de then
gu.Text = 0
i = 0
de = false
end
end
local gu = game.Workspace.w.SurfaceGui.TextLabel
local click = game.Workspace.c.ClickDetector
local de = false
click.MouseClick:Connect(function()
if de then
return
end
de = true
for i = gu.Text, 99 , 1 do
wait(1)
gu.Text += 1
if de then
gu.Text = 0
i = 0
de = false
end
end
end)
Just have everything inside the callback function connected to the mouse click event.
local gu = game.Workspace.w.SurfaceGui.TextLabel
local click = game.Workspace.c.ClickDetector
local de = false
click.MouseClick:Connect(function()
if de then
return
end
de = true
for i = gu.Text, 99 , 1 do
wait(1)
gu.Text += 1
if de then
gu.Text = 0
i = 0
end
end
de = false
end)
Didn’t realise your debounce was setup incorrectly, if you want the the loop to reset back to the start instead of using a debounce then let me know.
You were pretty close with your original implementation. One way to approach the problem is by implementing your for loop as a continuous loop.
local gu = game.Workspace.w.SurfaceGui.TextLabel
local click = game.Workspace.c.ClickDetector
local lastTimeClicked = nil
local lastTimeStepped = nil
click.MouseClick:Connect(function()
lastTimeClicked = os.clock()
end)
local RunService = game:GetService("RunService")
local progress = 0
RunService.Heartbeat:Connect(function(deltaTime)
if lastTimeClicked ~= lastTimeStepped then
-- lastTimeClicked as changed, reset our progress
lastTimeStepped = lastTimeClicked
progress = 0
end
-- increment our progress
progress = math.min(100, progress + deltaTime)
-- show it on our UI as an integer
gu.Text = string.format("%i", progress)
end)
The basic idea is that the loop will continue to run and check to see if lastTimeClicked has changed from the previous frame. If it has, we will reset the progress to 0 and store the lastTimeStepped as our current lastTimeClicked.
This has a few notes though. This method will represent progress as a float and not an integer. If it needs to be a whole number, you could introduce another variable every time you update progress like so: progressInt = math.floor(progress).
This loop will also always run in the background. If you only need this loop to run for a short time, you may want to consider connecting and disconnecting from the Heartbeat event dynamically.
This isn’t necessarily exactly what you’re asking for, though I think you’re looking for something like this:
local gu = game.Workspace.w.SurfaceGui.TextLabel
local click = game.Workspace.c.ClickDetector
local id = 0
local function startLoop()
id += 1
gu.Text = 0
-- Store the id for this loop
local loopId = id
for i = 0, 99, 1 do
wait(1)
-- If the id isn't the loop id, end loop
if loopId ~= id then
break
end
gu.Text += 1
end
end
click.MouseClick:Connect(function()
startLoop()
end)
startLoop()
If you’re looking to do exactly what you’re asking for, you can switch the for loop out for a while loop:
local startValue = 0
local endValue = 10
local increment = 1
-- For loop
for i = startValue, endValue, increment do
print(i)
end
-- While loop
local i = startValue
while i <= endValue do
print(i)
i = i + increment
end
Both of the loops do the same thing, but you can change the value of i with the while loop.