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)
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…
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)
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.
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
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
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.
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