Ok first things first, Problem:
while task.wait(1) do
Instead do this:
local LocalPlayer = game.Players.LocalPlayer
local hum = LocalPlayer.Character:WaitForChild("Humanoid")
hum:GetPropertyChangedSignal("Health"):Connect(function()
local health = hum.Health
end)
Now what this does is instead of checking every delayed second time and maybe lag the person’s game, this function will only fire if the player’s health goes down.
Incorporated Script:
local tweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local label = script.Parent.TextLabel
local LocalPlayer = Players.LocalPlayer
local greenFrame = script.Parent.HealthFront
local info = TweenInfo.new(.5,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)
local lastHealth = 0
local hum = LocalPlayer.Character:WaitForChild("Humanoid") -- Changes
hum:GetPropertyChangedSignal("Health"):Connect(function() -- Changes
if LocalPlayer.Character ~= nil then
local currentHealth = hum.Health
local newFrameSize = UDim2.fromScale(currentHealth/100, 1)
local tween = tweenService:Create(greenFrame,info,{Size=newFrameSize})
tween:Play()
if currentHealth > lastHealth then
for i = lastHealth, currentHealth, 1 do
label.Text = math.floor(i) .." / 100"
task.wait(1/(currentHealth-lastHealth))
end
else
for i = lastHealth, currentHealth, -1 do
label.Text = math.floor(i) .." / 100"
task.wait(1/(currentHealth-lastHealth))
end
end
lastHealth = currentHealth
end
end)
Now this would work but the script can be shortened a tiny bit.
local tweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local label = script.Parent.TextLabel
local LocalPlayer = Players.LocalPlayer
local greenFrame = script.Parent.HealthFront
local info = TweenInfo.new(.5,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)
local lastHealth = 0
local hum = LocalPlayer.Character:WaitForChild("Humanoid")
hum:GetPropertyChangedSignal("Health"):Connect(function()
if LocalPlayer.Character ~= nil then
local currentHealth = hum.Health
local newFrameSize = UDim2.fromScale(currentHealth/100, 1)
tweenService:Create(greenFrame,info,{Size=newFrameSize}):Play() -- Changes
if currentHealth > lastHealth then
for i = lastHealth, currentHealth, 1 do
label.Text = math.floor(i) .." / 100"
task.wait(1/(currentHealth-lastHealth))
end
else
for i = lastHealth, currentHealth, -1 do
label.Text = math.floor(i) .." / 100"
task.wait(1/(currentHealth-lastHealth))
end
end
lastHealth = currentHealth
end
end)
Now in this script what it does is it doesn’t use the “Tween Variable”. As said it is not necessary to use in this case since you only using it to play the tween, so instead of having 2 lines doing the same thing you can have 1 line that does the entire thing. I hope this works!