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”
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.
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