Tween not working

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
1 Like

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.

2 Likes

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

1 Like

What values are printed? If it’s over 1, and ClipDescendants is on, it won’t show, and it’ll simply look like it is not tweening.

2 Likes

The value of the changing stamina is printed each time when I do

print(Module.DataStore.Stamina)

Inside the function, but not anywhere outside of it. The problem is that even though the value itself is changing, my while loop isn’t picking it up.

What numbers are being printed though, could you give me an example or two of what is actually in the output?

1 Like

Sure, here’s my output after I utilized something that consumed stamina.image

Is the percentage going beneath 1, or is that what is attempted to be tweened to?

1 Like

Those numbers are what it’s supposed to be tweened to, but the bar doesn’t move at all.

Although, the percentage variable I put falls under one by default since anything divided by 100 that’s less than 100 would be less than 1

1 Like

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.

2 Likes

How would I go about Lerping it instead of tweening?

1 Like

Udim2 has a function where you are able to lerp. It is actually very easy to do.

Get the original Udim. This case it will be the size of the framt to begin with.

local Start = StaminiaBar.Size

Then we get the goal of our lerp.

local Goal = Udim2.fromScale(Percentage,1)

We can choose an arbitrary alpha(It will represent the “responsiveness” of the lerp) I will choose 0.5 Note: has to be between 0 and 1

local Alpha = 0.5

Then we can use the function to lerp and then apply the changes

StaminaBar.Size = Start:Lerp(Goal,Alpha)
2 Likes