Tween "lags" behind actual value in bar

External Media

As shown in the above video, the tween doesn’t finish as fast as the stamina value updates, creating a delay/lag between the bar and the actual value. The tween only ever catches up when the bar stops updating (when the stamina value reaches 100)

Relevant code:

local function UpdateFrame(Frame, Value, Limit)
	local Bar = Frame:WaitForChild("Bar")
	local Label = Frame:WaitForChild("Label")
	
	local Value = math.round(Value * 10) / 10
	local Limit = math.round(Limit * 10) / 10
	
	Label.Text = Frame.Name.." ("..Value.."/"..Limit..")"
	
	--[[local hue, saturation, value = Bar.BackgroundColor3:ToHSV()
	saturation = Value/Limit
	local NewColor = Color3.fromHSV(hue, saturation, value)]]
	
	local BarTweenInfo = TweenInfo.new(0.2,Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)
	local BarTween = TweenService:Create(Bar, BarTweenInfo, {Size = UDim2.fromScale(Value/Limit,1)})
	BarTween:Play()
end

Player:GetAttributeChangedSignal("Stamina"):Connect(function()
	local Stamina = Player:GetAttribute("Stamina")
	UpdateFrame(StaminaFrame, Stamina.X, Stamina.Y)
end)

-- A SEPERATE SERVER SCRIPT:

local LastChecked = time()
	local LastStamina = Player:GetAttribute("Stamina")

	Player:GetAttributeChangedSignal("Stamina"):Connect(function()
		local Stamina = Player:GetAttribute("Stamina")

		if Stamina.X < LastStamina.X then
			LastChecked = time()
		end
		
		LastStamina = Stamina
	end)

	RunService.Heartbeat:Connect(function(DT)
		local Stamina = Player:GetAttribute("Stamina")
		if time() - LastChecked > 3 and Stamina.X < Stamina.Y then
			Player:SetAttribute("Stamina", Vector2.new(math.clamp(Stamina.X + 8 * DT,0,Stamina.Y),Stamina.Y))
		end
	end)

How do I make it so the gui bar doesn’t lag behind the actual stamina value?

Maybe what is happening is that its canceling other tweens, so what i would do is to update the tween every “.5” in the health value so it doesnt look “laggy”

Was this post made using AI?
Cause “Tween” isn’t an object in the bar, it’s made from TweenService.

i dont think tweens makes an instance and then parents it to the part/ui element…

1 Like

so an example of this would be like

local _int, float = math.modf(healthValue) -- This returns the integer (like 1, 2, 3) and the float (like .5, .2, .75)

if float == .5 then
    --  Create and play the tween
end

I don’t think this would work, as some moves don’t create a float. For example, the teleport move shown in the video subtracts stamina by 10, which leaves a float of 0. The code you mentioned wouldn’t fire in cases like that.

well in that case u can just add the 0 to the if statement i dont see that as a big problem

I’ve found a “temporary” solution by checking the change between the old and new stamina. If the change is greater than one, it tweens the bar’s size because it won’t be delayed. Otherwise, it simply updates the size of the bar without tweening.

Updated Code:

local function UpdateFrame(Frame, Value, Limit)
	local StaminaChange = math.abs(OldStamina.X - Value)

	local Bar = Frame:WaitForChild("Bar")
	local Label = Frame:WaitForChild("Label")

	local Value = math.round(Value * 10) / 10
	local Limit = math.round(Limit * 10) / 10

	Label.Text = Frame.Name.." ("..Value.."/"..Limit..")"

	--[[local hue, saturation, value = Bar.BackgroundColor3:ToHSV()
	saturation = Value/Limit
	local NewColor = Color3.fromHSV(hue, saturation, value)]]

	if StaminaChange > 1 then
		local BarTweenInfo = TweenInfo.new(0.2,Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)
		local BarTween = TweenService:Create(Bar, BarTweenInfo, {Size = UDim2.fromScale(Value/Limit,1)})
		BarTween:Play()
	else
		Bar.Size = UDim2.fromScale(Value/Limit,1)
	end
end
1 Like

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