What do you want to achieve? Keep it simple and clear!
I’m making a game similar to Raise a Floppa. I want to use a UIGradient’s offset to change the “size” of the bar depending on how much hunger I have. The hunger value starts at 100 and slowly decreases to 0.
What is the issue? Include screenshots / videos if possible!
For example, in order to get an empty hunger bar using UIGradient, the X offset has to be -1. But that means that the value of 0 will have to turn into -1 and I can’t figure out that math.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried to turn the hunger value to a negative number and divide that by 100 to get a negative decimal. While it does show correctly when the hunger is at 50, a hunger value of 40 makes the UIGradient’s offset -0.4, which makes the bar fill up to around 60% of its size.
Code for the bar tweening, local script is placed inside of the hunger bar.
local players = game:GetService("Players")
local player = players.LocalPlayer
local statsFolder = player:WaitForChild("FriendStats")
local hungerVal = statsFolder:WaitForChild("Hunger")
local tweenService = game:GetService("TweenService")
local ti = TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local bar = script.Parent
local hungerText = bar.Parent.TextLabel
local uiGradient = bar:FindFirstChild("UIGradient")
hungerVal:GetPropertyChangedSignal("Value"):Connect(function()
if hungerVal.Value <= 0 then
bar.Visible = false
else
bar.Visible = true
end
local gradientTween = tweenService:Create(uiGradient, ti, {Offset = Vector2.new(-(hungerVal.Value / 100), 0)})
gradientTween:Play()
hungerText.Text = hungerVal.Value.. "%"
end)