Slowly reducing value of an IntValue

I’m using an IntValue to increase a GUI that converts the whole number into a percentage and changes the GUI’s size. I have completed that and it works perfectly. My next task was to create a drainage where it slowly takes away a specific value from the IntValue until it reaches zero. I attempted to do this with a while loop which ended up doing nothing. I combined it with the Changed event and it ended up changing it but it would double the decreased value after each run through. Under is the current code I have that does not work.

while scalingnumber.Value > 0 do
	wait(1)
	scalingnumber.Value = math.max(scalingnumber.Value - 5, 0)
end

Where is this code run? Is it possible it’s in a function that gets called twice?

Also why not just do

while scalingnumber.Value >5 do
    task.wait(1)  --task.wait is better than wait
    scalingnumber -= 5
end
1 Like

the code is ran inside of the GUI im changing within the local script that changes it arleady.
reason i didnt do what you provided is because when i attempted to get it to work, it would double and end up going into the negatives causing the gui to go backwards when it reaches 0. the code is after the function inside a script that is called once. The script below is the entire script inside of the local script that changes the size of the GUI based on the number inputed within the IntValue. Im aware this may not be the most efficient way either.

local gui = game.Players.LocalPlayer.PlayerGui.FeverBar.FeverContainer.Background.FeverBar

local scalingnumber = script.FeverAmount

local function valuechanged()
	local tweenservice = game:GetService("TweenService")
	local tweeninfo = TweenInfo.new(0.5,Enum.EasingStyle.Linear)

	local percent = scalingnumber.Value / 100

	local goal = {}
	goal.Size = UDim2.new(1,0,percent,0)

	local tween = tweenservice:Create(gui, tweeninfo, goal)
	tween:Play()
	
end
	
while scalingnumber.Value > 0 do
	wait(1)
	scalingnumber.Value = math.max(scalingnumber.Value - 5, 0)
end
	

scalingnumber.Changed:Connect(valuechanged)
local gui = game.Players.LocalPlayer.PlayerGui.FeverBar.FeverContainer.Background.FeverBar

local scalingnumber = script.FeverAmount

local function valuechanged()
	local tweenservice = game:GetService("TweenService")
	local tweeninfo = TweenInfo.new(0.5,Enum.EasingStyle.Linear)

	local percent = scalingnumber.Value / 100

	local goal = {}
	goal.Size = UDim2.new(1,0,percent,0)

	local tween = tweenservice:Create(gui, tweeninfo, goal)
	tween:Play()
	
end

scalingnumber.Changed:Connect(valuechanged)

while scalingnumber.Value >= 0 do
	task.wait(1)
	scalingnumber.Value = math.max(scalingnumber.Value - 5, 0)
print("decreased by 5")
end

fixed the issue. needed an equal sign lol

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