While loop wont restart and interval is incorrect (on restart)

local Bar = script.Parent:WaitForChild("Bar")

local IntervalDisplay = script.Parent:WaitForChild("Top"):WaitForChild("Interval")

while true do
	
	local Interval = game:GetService("ReplicatedStorage"):WaitForChild("GrowthIntervalInfo"):WaitForChild("Interval")
	
	IntervalDisplay.Text = tostring(Interval.Value)
	
	Bar:TweenSize(UDim2.new(0,0,0.025,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Linear,60)
	
	task.wait(60)
	
	Bar:TweenSize(UDim2.new(1,0,0.025,0))
	
end

The interval will be displayed as the previous and the bar will not shrink when it restarts for the new interval

3 Likes

I don’t understand what the problem is. Also, do not use :GetService() multiple times. (Especially in a while loop) Define ReplicatedStorage at the beginning of your code.

The problem is that once the 60 second tween ends when the loop restarts it wont display the correct interval nor will it start the 60 second tween again

So when does it tween back after the first tween?

Try to do this

local Bar = script.Parent:WaitForChild("Bar")

local IntervalDisplay = script.Parent:WaitForChild("Top"):WaitForChild("Interval")

local Interval = game:GetService("ReplicatedStorage"):WaitForChild("GrowthIntervalInfo"):WaitForChild("Interval")

while true do
	task.wait(0.1)
	
	IntervalDisplay.Text = tostring(Interval.Value)

	Bar:TweenSize(UDim2.new(1,0,0.025,0))
repeat wait() until Bar.Size = UDim2.new(1,0,0.025,0)
	
	Bar:TweenSize(UDim2.new(0,0,0.025,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Linear,60)
	
	task.wait(60)	
end

If the Interval doesn’t change while the 60 seconds loop is running, then you can do this outside and before the loop:

Interval:GetPropertyChangedSignal("Value"):Connect(function()
IntervalDisplay.Text = tostring(Interval.Value)
end)
Bar:TweenSize(UDim2.new(1,0,0.025,0))

i cannot have it fire everytime the interval changes bc that would look laggy also i dont want the repeat wait bc if theres an issue then it will be too long

Sorry, what I meant to say is does it tween back?

yes, extra text here 123 extra extra…

whats bad about doing this?

extra text

It is bad because it slows down your script every time you use the method. You are recalling the service multiple times which could be defined at the beginning of your script. You continuously reload all of the service’s properties every time you use it, when you could use a variable outside the loop.