Health bar tween break when loosing too much health

When i loose too much health, it wont tween to the right size

his is the code for the bar

local TweenService = game:GetService("TweenService")


local Script = script
local Players = game:GetService("Players")
local Character = Script.Parent
local Player = Players:GetPlayerFromCharacter(Character)
local Run = game:GetService("RunService")

local bar = Character.Head.OverheadGUI.HealthBar.Bar

local event = game.ReplicatedStorage.RE.LowHealth


Player.Character.Humanoid.HealthChanged:Connect(function()
	if Player.Character.Humanoid.Health < 21 then
		event:FireClient(Player, true, Player.Character.Humanoid.Health)
	else
		event:FireClient(Player, false)
	end
	
	local barheath = Player.Character.Humanoid.Health * 2
	
	print(barheath)
	
	--bar.Size = UDim2.new(0, barheath, 0, 16)
	
	bar:TweenSize(UDim2.new(0, barheath, 0, 16)) -- What i want to fix
	
	bar.ImageColor3 = Color3.new(1, 0, 0):Lerp(Color3.new(0, 1, 0), Player.Character.Humanoid.Health / 100)
	
end)

Thanks

5 Likes

You need to make the bar’s size relative to the MaxHealth of the Humanoid.

local barheath = Player.Character.Humanoid.Health / Player.Character.Humanoid.MaxHealth

This returns a value between 0 and 1 with 0 being no health and 1 being max health.
You can then use this for the bar’s scale because the scale is also from 0 to 1.

2 Likes

the bars size is 200, so i just divide the size by 2

2 Likes

wait i think i see what your getting at hold on

2 Likes

You can use the Scale for UDim2 rather than the offset.
UDim2.new(ScaleX, OffsetX, ScaleY, OffsetY)
The scale is based on the size of the element’s parent so a scale of 0.5 for the X would make the object half the size of its parent.

2 Likes

well that just made the bar dissapear on any damage, like i said before i want to fix this:

	bar:TweenSize(UDim2.new(0, barheath, 0, 16)) -- What i want to fix
2 Likes

Everything’s working fine on my end. Maybe a replication problem?

1 Like

Whats happening is im loosing health multiple times, so im not loosing 100 health, im loosing 10, 10, 10, 10 ect
and what i think is happening is the tween only plays a few times, and it wont go to the proper percent

2 Likes

I’m not seeing any issue with losing health repetitively either.

Could you possibly send a demo .rblx place with the problematic setup so I could properly debug the entire system?

1 Like

GameForACoolGuy.rbxl (154.2 KB)

1 Like

Seems to be an issue with TweenSize() not updating correctly. I would highly recommend avoiding TweenSize() and TweenPosition() as I have encountered people that have experienced similar issues with them. Instead, you can use TweenService which is much more reliable and overall more customizable.


local TweenService = game:GetService("TweenService")


local Script = script
local Players = game:GetService("Players")
local Character = Script.Parent
local Player = Players:GetPlayerFromCharacter(Character)
local Run = game:GetService("RunService")

local bar = Character.Head.OverheadGUI.HealthBar.Bar

local event = game.ReplicatedStorage.RE.LowHealth

local info = TweenInfo.new(
	
	1,
	Enum.EasingStyle.Cubic
	
)


Player.Character.Humanoid.HealthChanged:Connect(function()
	if Player.Character.Humanoid.Health < 21 then
		event:FireClient(Player, true, Player.Character.Humanoid.Health)
	else
		event:FireClient(Player, false)
	end
	
	local barheath = Player.Character.Humanoid.Health * 2
	
	
	print(barheath)
	
	--bar.Size = UDim2.new(0, barheath, 0, 16)
	TweenService:Create(bar, info, {Size = UDim2.new(0, barheath, 0, 16)}):Play()
	
	bar.ImageColor3 = Color3.new(1, 0, 0):Lerp(Color3.new(0, 1, 0), Player.Character.Humanoid.Health / 100)
	
end)
1 Like

YIPEEEE IT WORKED
on more question, in the test game can you help me figure out how to make the red viginette slowly fade in instead of just appearing? thats fine if you dont wanna help with that though, as that wasnt the original point of the forum

1 Like

you can also use tweenservice for the red , unless you’re talking about the coregui one in which you could make your own system using tweenservice

1 Like

im new to tweenservice, how would i use it to do this? also im talking about an in-game one that i have, similar to other games

1 Like

You can tween the images transparency to 0

1 Like

I rewrote both the server and the local script so here’s both:
Server:


local TweenService = game:GetService("TweenService")


local Script = script
local Players = game:GetService("Players")
local Character = Script.Parent
local Player = Players:GetPlayerFromCharacter(Character)
local Run = game:GetService("RunService")

local bar = Character.Head.OverheadGUI.HealthBar.Bar

local event = game.ReplicatedStorage.RE.LowHealth

local info = TweenInfo.new(
	
	1,
	Enum.EasingStyle.Cubic
	
)


Player.Character.Humanoid.HealthChanged:Connect(function()
	event:FireClient(Player, Player.Character.Humanoid.Health)
	
	local barheath = Player.Character.Humanoid.Health * 2
	
	
	print(barheath)
	
	--bar.Size = UDim2.new(0, barheath, 0, 16)
	TweenService:Create(bar, info, {Size = UDim2.new(0, barheath, 0, 16)}):Play()
	
	bar.ImageColor3 = Color3.new(1, 0, 0):Lerp(Color3.new(0, 1, 0), Player.Character.Humanoid.Health / 100)
	
end)

Local:

local TweenService = game:GetService("TweenService")

local even = game.ReplicatedStorage.RE.LowHealth
local Frame = script.Parent.ImageLabel

local info = TweenInfo.new(
	
	1,
	Enum.EasingStyle.Cubic
	
)

even.OnClientEvent:Connect(function(Amount)
	TweenService:Create(Frame, info, {ImageTransparency = Amount / 21}):Play()
end)

1 Like

ill try it, its funny you still called my event “even” because that was a spelling error lol

Don’t forget barheath lol.

char limit rahhhh

1 Like

well ofc the first script works, but the local one wont? to check i SHOULD be putting it in starterGUI, Lowhealth (the directory to the effect)

1 Like

I just changed this script and I didn’t move it so I’m not sure why it wouldn’t work.
image

1 Like