I have a ‘for’ loop that is supposed to play 100 times and reduce the current number by 100 and drop by 1 every 0.1 of a second. But when I press the button the number only drops by 1 and doesn’t play anymore. Any help appreciated
script.Parent.ClickDetector.MouseClick:Connect(function()
local Tempt = game.Workspace.GlobalVariables.Temp.Value
print("Current Tempt: " .. Tempt)
local MinusTempt = 100
print("Minus Tempt: " .. MinusTempt)
local FinalTempt = Tempt - MinusTempt
print("Final Tempt: " .. FinalTempt)
for i = Tempt, FinalTempt, -MinusTempt do -- -MinusTempt
local Tempt2 = Tempt - 1
wait(0.01)
game.Workspace.TEMPMONITOR.Part.SurfaceGui.TextBox.Text = Tempt2 .. "F"
game.Workspace.GlobalVariables.Temp.Value = Tempt2
end
end)
It just immediately went from 1000 changed to 999 then just went to 899 it didn’t gradually reduce the 1 it just straight up reduced by 100. Is this supposed to happen or am I just not getting this
The loop stops because your decrement is -100, also known as -MinusTempt. The loop only runs 2 times since it removes 100 and then immediately reaches FinalTempt and then breaks out of the loop. If you want the loop to run 100 times the decrement should be -1 and your FinalTempt should be FinalTempt +1.
for i = 1, 100 do
Tempt -= 1
Tempt2 -= 1
wait(0.01)
game.Workspace.TEMPMONITOR.Part.SurfaceGui.TextBox.Text = Tempt2 .. "F"
game.Workspace.GlobalVariables.Temp.Value = Tempt2
if Tempt 1 <= 0 or Tempt2 <= 0 then
break -- in case of loop not stopping
end
end
end)
Edit: didnt read right, ill provide better solution on this post later
local Tempt = game.Workspace.GlobalVariables.Temp
local MinusTempt = 100
local FinalTempt
local db = true -- debounce so that you won't spam click with only 1 click
script.Parent.ClickDetector.MouseClick:Connect(function()
if db == true then db = false
print("Current Tempt: " .. Tempt.Value)
print("Minus Tempt: " .. MinusTempt)
FinalTempt = Tempt.Value - MinusTempt
print("Final Tempt: " .. FinalTempt)
local bigDrop = Tempt.Value - MinusTempt
for Tempt2 = Tempt.Value,bigDrop,-1 do
game.Workspace.TEMPMONITOR.Part.SurfaceGui.TextBox.Text = Tempt2 .. "F"
game.Workspace.GlobalVariables.Temp.Value = Tempt2
wait(0.01)
end
task.wait(2) -- wait seconds before you can click again
db = true
end
end)