Alright, iβll try to make a sprite sheet and see if it works.
--//Services//--
local Players = game:GetService("Players")
local ContentProvider = game:GetService("ContentProvider")
--//Variables
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local PlayerGui = script.Parent
local MainGui = PlayerGui:WaitForChild("MainGui")
local HealthMeter = MainGui.HealthMeter
local TweenService = game:GetService("TweenService")
local TweenInform = TweenInfo.new(0.25, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
--------------------------------------
local SpriteSize = Vector2.new(37, 50)
local SpaceSize = Vector2.new(2, 2)
local Sprites = {
HM1 = Vector2.new(0, 0),
HM2 = Vector2.new(37, 0),
HM3 = Vector2.new(74, 0),
HM4 = Vector2.new(0, 52),
HM5 = Vector2.new(37, 52),
HM6 = Vector2.new(74, 52),
HM7 = Vector2.new(0, 104),
HM8 = Vector2.new(37, 104),
HM9 = Vector2.new(74, 104)
}
--//Function//--
local function switchHealthMeter(Meter)
local Location = Sprites[Meter]
HealthMeter.ImageRectSize = SpriteSize
HealthMeter.ImageRectOffset = (SpriteSize + SpaceSize) * Location
HealthMeter.Size = UDim2.new(0, 120, 0, 160)
local TweenHM = TweenService:Create(HealthMeter, TweenInform, {Size = UDim2.new(0,100,0,140)})
TweenHM:Play()
end
---------------------------------------------------------
local function onHealthChanged(Health)
if Health > 90 and Health < 100 or Health == 100 or Health == 90 then
switchHealthMeter("HM1")
elseif Health >= 80 and Health < 90 then
switchHealthMeter("HM2")
elseif Health >= 70 and Health < 80 then
switchHealthMeter("HM3")
elseif Health >= 60 and Health < 70 then
switchHealthMeter("HM4")
elseif Health >= 50 and Health < 60 then
switchHealthMeter("HM5")
elseif Health >= 40 and Health < 50 then
switchHealthMeter("HM6")
elseif Health >= 30 and Health < 40 then
switchHealthMeter("HM7")
elseif Health >= 1 and Health < 30 then
switchHealthMeter("HM8")
elseif Health <= 0 then
switchHealthMeter("HM9")
end
end
--//Connection//--
Humanoid.HealthChanged:Connect(onHealthChanged)
I made this using a tutorial, and it does work, but, wellβ¦
It looks like this when i lose health. I think it has something to do with the ScaleType?
Can you send here image you used for sprite sheet? Issue above occurs when you set RectOffset to be bigger than image itself.
(Also, SpaceSize you did not accounts for X axis)
I cant help it but plz consolidate ur code
local healthMeters = {
{range = {91, 100}, meter = "HealthMeter1"},
{range = {80, 90}, meter = "HealthMeter2"},
{range = {70, 80}, meter = "HealthMeter3"},
{range = {60, 70}, meter = "HealthMeter4"},
{range = {50, 60}, meter = "HealthMeter5"},
{range = {40, 50}, meter = "HealthMeter6"},
{range = {30, 40}, meter = "HealthMeter7"},
{range = {1, 30}, meter = "HealthMeter8"},
{range = {0, 0}, meter = "HealthMeter9"},
}
local function switchHealthMeter(meterName)
for __, img in pairs(HMFolder:GetChildren()) do
img.Visible = img.Name == meterName
if img.Visible then
img.Size = UDim2.new(0, 120, 0, 160)
local tweenHM = TweenService:Create(img, TweenInform, {Size = UDim2.new(0,100,0,140)})
tweenHM:Play()
end
end
end
---------------------------------------------------------
local function onHealthChanged(health)
for _, meterInfo in ipairs(healthMeters) do
if health >= meterInfo.range[1] and health <= meterInfo.range[2] then
switchHealthMeter(HMFolder[meterInfo.meter])
break
end
end
end
--//Connection//--
Humanoid.HealthChanged:Connect(onHealthChanged)
Ok but for ur actual problem i would modify switch health meter so that the ui is visible before u tween itβ also just manage it a bit more nicely:
Try dis
local function switchHealthMeter(Meter)
for __, Img in pairs(HMFolder:GetChildren()) do
if Img.Name == Meter.Name then
Img.Visible = true
Img.Size = UDim2.new(0, 120, 0, 160) -- Ensure size is reset before tween
--Wait for previous tween
if Img.Tween then
Img.Tween:Cancel()
end
local TweenHM = TweenService:Create(Img, TweenInform, {Size = UDim2.new(0,100,0,140)})
Img.Tween = TweenHM --U can store the tween if u need to use it later
TweenHM:Play()
TweenHM.Completed:Connect(function()
Img.Tween = nil -- Clear the reference once the tween is completed
end)
else
Img.Visible = false
end
end
end
It says Tween is not a valid member of HealthMeter (Img) tho
Bump β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β
The error message explains it all. βTweenβ is and was never a valid member of Img, so I donβt know where Polynovuh got that solution from.
Not only that, it was completely unnecessary to store the tween because youβre already calling TweenService:Create every time the function is called. If you really care about that and donβt want to create a new tween every single time, you can create it outside the function, store it in a variable, and then call variable:Play().
Also, have you tried my solution (setting .ImageTransparency instead of .Visible)?
--//Variables//--
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local PlayerGui = script.Parent
local MainGui = PlayerGui:WaitForChild("MainGui")
local HMFolder = MainGui.HealthMeters
local TweenService = game:GetService("TweenService")
local TweenInform = TweenInfo.new(0.25, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
--//Function//--
local function switchHealthMeter(Meter)
for __, Img in pairs(HMFolder:GetChildren()) do
if Img.Name == Meter.Name then
Img.ImageTransparency = 0
Img.Size = UDim2.new(0, 120, 0, 160)
local TweenHM = TweenService:Create(Img, TweenInform, {Size = UDim2.new(0,100,0,140)})
TweenHM:Play()
else
Img.ImageTransparency = 1
end
end
end
---------------------------------------------------------
local function onHealthChanged(Health)
if Health > 90 and Health < 100 or Health == 100 or Health == 90 then
switchHealthMeter(HMFolder.HealthMeter1)
elseif Health >= 80 and Health < 90 then
switchHealthMeter(HMFolder.HealthMeter2)
elseif Health >= 70 and Health < 80 then
switchHealthMeter(HMFolder.HealthMeter3)
elseif Health >= 60 and Health < 70 then
switchHealthMeter(HMFolder.HealthMeter4)
elseif Health >= 50 and Health < 60 then
switchHealthMeter(HMFolder.HealthMeter5)
elseif Health >= 40 and Health < 50 then
switchHealthMeter(HMFolder.HealthMeter6)
elseif Health >= 30 and Health < 40 then
switchHealthMeter(HMFolder.HealthMeter7)
elseif Health >= 1 and Health < 30 then
switchHealthMeter(HMFolder.HealthMeter8)
elseif Health <= 0 then
switchHealthMeter(HMFolder.HealthMeter9)
end
end
--//Connection//--
Humanoid.HealthChanged:Connect(onHealthChanged)
Make sure to make all your healthbar image visible with ImageTransparency set to 1 though
I just did and it still wonβt workβ¦
Wdym not working? is it the same issue image not tweening? or some error
No error, it just wonβt work for some reason.
Bumpβ β β β β β β β β β β β β β β β β β β β β β β β
Bruh,that makes the code so terrible and it eill run a lot of times
Could you elaborate please? β β β β β β β β β β β β β β β β β β β β β β β
Bumpβ β β β β β β β β β β β β β β β β β β β β β β β
The last part is the error,you check whether Health even exsist
What last part are you talking about?
Sorry for the late response, here it is.
Try this function instead of your one:
local function switchHealthMeter(Meter)
local Location = Sprites[Meter]
local Id = tonumber(string.match(Meter, "HM(%d+)"))
local X = (Id-1)%3
local Y = math.floor((Id-1)/3)
HealthMeter.ImageRectSize = SpriteSize
HealthMeter.ImageRectOffset = (SpriteSize + SpaceSize) * Vector2.new(X, Y)
HealthMeter.Size = UDim2.new(0, 120, 0, 160)
local TweenHM = TweenService:Create(HealthMeter, TweenInform, {Size = UDim2.new(0,100,0,140)})
TweenHM:Play()
end
(Assuming that you still use sprite sheet)