For loop only plays once

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) 

What is the goal of this code (context, what does it do?)?

1 Like

While the loop is running, you are subtracting Tempt (not changing) by 1, 100 times, not i which is being incremented by -MinusTempt.

To solve your issue, you should change this line to local Tempt2 = i - 1

Let me know how you make out.

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

Can you please send a screenshot of the console output when you click? I want to see what is being printed.

console

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.

1 Like

For what i assume is Tempt and Tempt2

	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

^ This should be the solution @DiamondSwordOfScript :+1:

1 Like
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) 

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.