How do i create a *Maximum Value* for while loop

  1. What do you want to achieve? Keep it simple and clear!

i want to create a maximum value for my script for the while loop to stop
and then theres a cooldown and the number resets to 0.
after the cooldown the while loop will start again.

What the script is about: when hold it will add a value, until its max value it will reset.

local hold = false
local UIS = game:GetService("UserInputService")
local maxValue = 0.5
local Cooldown = 3 
local currentValue = 0

UIS.InputBegan:Connect(function(input, gameProccesed)
	if gameProccesed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		hold = true

		while hold do
			currentValue += 0.01
			wait(.1)

			print(currentValue)
		end
	end
end)

UIS.InputEnded:Connect(function(input, gameProccesed)
	if gameProccesed then
		return
	end
	
	hold = false
end)
4 Likes

You can check if the value is higher or equal to the maximum value then reset it

UIS.InputBegan:Connect(function(input, gameProccesed)
	if gameProccesed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		hold = true
	end
end)

task.spawn(function() — placing while loop outside InputBegan
while true do
task.wait(.1)
if hold then 
currentValue += 0.01
print(currentValue)
if currentValue >= maxValue then — check if value exceeded maxValue
currentValue = 0
task.wait(Cooldown) — cooldown
end
end
end
end)
— rest of code…
2 Likes

Use repeat until.

Example:

local ElapsedTime = 0

-- Wait 1 second, add one to ElapsedTime until it equals 10
repeat
    task.wait(1)
    ElapsedTime += 1
until ElapsedTime == 10
2 Likes

or just do this

local ElapsedTime = 0

repeat
    ElapsedTime += task.wait()
until ElapsedTime >= 10
2 Likes

This should do exactly that:

local hold = false
local UIS = game:GetService("UserInputService")
local maxValue = 0.5
local Cooldown = 3 
local currentValue = 0

local coolingDown = false
UIS.InputBegan:Connect(function(input, gameProccesed)
	if gameProccesed or coolingDown then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		hold, coolingDown = true, true

		while hold do
			currentValue += 0.01
			wait(.1)

			print(currentValue)
			if currentValue >= maxValue then
				currentValue = maxValue --set this back to minimum value if you want to
				wait(Cooldown)
				coolingDown = false
				break
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input, gameProccesed)
	if gameProccesed then
		return
	end

	hold = false
end)
4 Likes

This has a few issues,
readability isn’t the best and performance issues may arise.

Your method has it run every tick (or something else) and the final ElapsedTime variable doesn’t exactly equal to 10 so if you want to check afterwards if its exactly 10 or if you’re using integers, you can’t because its something like 10.014636299999609.

It’d just be better to add one every second where it runs way less times and the resulting value is exactly 10.

3 Likes

task.wait() isn’t consistent, so there are instances where the elapsed time is 10 but the actual elapsed time will be approximately 11 (or rarely, more) because of the decimals

and that’s also why i replaced == with >=

you can use math.floor() for that

3 Likes

if you need something simple, this should work i think?

local hold = false
local UIS = game:GetService("UserInputService")
local maxValue = 0.5
local Cooldown = 3 
local currentValue = 0

UIS.InputBegan:Connect(function(input, gameProccesed)
	if gameProccesed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		hold = true

		while hold do
			currentValue += 0.01
			wait(.1)

			print(currentValue)
			if currentValue >= maxValue then
				hold = false
				currentValue = maxValue
				task.wait(1) --Cooldown
				currentValue = 0
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input, gameProccesed)
	if gameProccesed then
		return
	end
	
	hold = false
end)

But this could also break i feel like, maybe seperating these 2 to somewhere else could be better

while hold do
	currentValue += 0.01
	wait(.1)

	print(currentValue)
	if currentValue >= maxValue then
		hold = false
		currentValue = maxValue
		task.wait(1) --Cooldown
		currentValue = 0
	end
end
1 Like

That coolingDown variable is for debounce. There’s a rare possibility that the input is registered twice or more and results in the value increasing faster than normal.

1 Like

this works but when i stopped holding it wont print anymore also, when it breaks it couldnt print anymore, my target was to make it so that it resets to 0 then you could still hold and print the value, sorry i didnt make it clear

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