I’m trying to update my Stamina bar with a tween based off of data from a module, but it’s not working. When I print the Data from the module, it shows up correctly, but the stamina bar isn’t tweening at all. I can’t tell what’s going on or what I’m missing.
-- Variables --
local Module = require(game.StarterPlayer.StarterPlayerScripts.LocalStorage)
local Player = game.Players.LocalPlayer
local UserID = Player.UserId
local Char = Player.Character or Player.CharacterAdded
local StaminaBar = script.Parent
local Remote = game.ReplicatedStorage.StamBar
-- Settings --
local TweenSpeed = 1
-- Script --
local function UpdateBar()
local Percentage =
(Module.DataStore.Stamina/Module.DataStore.MaxStamina)
StaminaBar:TweenSize(UDim2.new(Percentage,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,TweenSpeed)
print(Module.DataStore.Stamina.. " | Stamina!")
end
Remote.OnClientEvent:Connect(function(Stamina,MaxStamina)
if Stamina ~= Module.DataStore.Stamina or Module.DataStore.Stamina == nil then
Module.DataStore.Stamina = Stamina
end
if MaxStamina ~= Module.DataStore.MaxStamina or Module.DataStore.MaxStamina == nil then
Module.DataStore.MaxStamina = MaxStamina
end
end)
while Module.DataStore.Stamina < Module.DataStore.MaxStamina do
wait()
UpdateBar()
end
I might be wrong about this, but I think requiring a ModuleScript at the beginning locks all the values (the same way you would set something like Humanoid.Health once and it won’t update), so you may want to require it multiple times in the script.
I tried that, but it doesn’t seem to work. The value changes, since when I print it inside the function, it’ll show, but when I try to print it outside, it wont print at all. It isn’t running the while loop for some reason
I don’t recommend you use Tween for this. Since the code is run in a while loop I would lerp the size. In my experience Lerping feels a little more “reactive” than smoothly tweening. Plus you get the added benefit of changing the value mid moving.
Also, if I am not mistaken it looks like you should probably have put a white true do loop instead.