Health script doesnt work

  1. It uses RunService for wait()
  2. It uses it properly for wait().
  3. Are there any errors?

there are no errors what so ever; in any of the scripts

same problem occurring since the beginning; health bar wont appear

Well you need to add your transparency tween i didn’t add it since you already have what you need. Replace teh comments with your transparency tween.

1 Like

same problem, here is the code:

local runService = game:GetService("RunService")
local players = game:GetService("Players")
local character = players.LocalPlayer.Character
repeat runService.Heartbeat:Wait() until players.LocalPlayer
local humanoid = character.Humanoid

runService.Heartbeat:Connect(function()
	local hp = game.Players.LocalPlayer.Character.Humanoid.Health/100
	script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.In,Enum.EasingStyle.Bounce,0.15)
	runService.Heartbeat:Wait()
end)

local TweenService = game:GetService("TweenService")
local HealthBar = players.LocalPlayer.PlayerGui.Health.HealthFrame.HealthBar
local HealthBackground = players.LocalPlayer.PlayerGui.Health.HealthFrame
HealthBackground.Transparency = HealthBackground.Transparency

local tweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.In, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tween = TweenService:Create(HealthBar, tweenInfo, {Transparency = 1})

HealthBackground.Transparency = HealthBackground.Transparency

local tweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.In, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tween2 = TweenService:Create(HealthBackground, tweenInfo, {Transparency = 1})

local tweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.In, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tween3 = TweenService:Create(HealthBar, tweenInfo, {Transparency = 0})


local tick_ = os.clock()
local thread_exists = false

humanoid.HealthChanged:Connect(function(health)
	tick_ = os.clock()
	HealthBar:TweenSize(UDim2.new(health/100,0,1,0), "Out", "Linear", 0.1, true)
	if not thread_exists then
		thread_exists = true
		
			tween3:Play()
		while math.abs(os.clock()-tick_)<=3 do -- a 3 second wait time to set to invisible
			wait()
		end
		tween:Play()
		tween2:Play()
		thread_exists = false
	end
end)

Firstly you can remove the runService.Heartbeat connection as it’ll do the same as the health changed thing. Then you can remove all the local tweenInfo declarations. Finally here we will declare what tweenInfo will do:

local tween1 = TweenService:Create(HealthBar, TweenInfo.new(1), {Transparency = 1})
local tween2 = TweenService:Create(HealthBackground, TweenInfo.new(1), {Transparency = 1})
local tween3 = TweenService:Create(HealthBackground, TweenInfo.new(1), {Transparency = 0})
local tween4 = TweenService:Create(HealthBar, TweenInfo.new(1), {Transparency = 0})
-- now tween 1 and 2 will make bar invisible, and tween 3 and 4 will make it visible

like this?:

local runService = game:GetService("RunService")
local players = game:GetService("Players")
local character = players.LocalPlayer.Character
repeat runService.Heartbeat:Wait() until players.LocalPlayer
local humanoid = character.Humanoid

	local hp = game.Players.LocalPlayer.Character.Humanoid.Health/100
	script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.In,Enum.EasingStyle.Bounce,0.15)
	runService.Heartbeat:Wait()

local TweenService = game:GetService("TweenService")
local HealthBar = players.LocalPlayer.PlayerGui.Health.HealthFrame.HealthBar
local HealthBackground = players.LocalPlayer.PlayerGui.Health.HealthFrame
HealthBackground.Transparency = HealthBackground.Transparency

local tween1 = TweenService:Create(HealthBar, TweenInfo.new(1), {Transparency = 1})
local tween2 = TweenService:Create(HealthBackground, TweenInfo.new(1), {Transparency = 1})
local tween3 = TweenService:Create(HealthBackground, TweenInfo.new(1), {Transparency = 0})
local tween4 = TweenService:Create(HealthBar, TweenInfo.new(1), {Transparency = 0})
-- now tween 1 and 2 will make bar invisible, and tween 3 and 4 will make it visible


local tick_ = os.clock()
local thread_exists = false

humanoid.HealthChanged:Connect(function(health)
	tick_ = os.clock()
	HealthBar:TweenSize(UDim2.new(health/100,0,1,0), "Out", "Linear", 0.1, true)
	if not thread_exists then
		thread_exists = true
			tween3:Play()
			tween4:Play()
		while math.abs(os.clock()-tick_)<=3 do -- a 3 second wait time to set to invisible
			wait()
		end
		tween1:Play()
		tween2:Play()
		thread_exists = false
	end
end)

still doesnt work

here is the gui layout so anyone can test this themselves: Screen Shot 2021-04-13 at 7.11.46 PM

back to this script: is there any obvious erros that could be causing the tween not to play?

Dude. You have a while true loop before the rest of your code. The rest of your code won’t run! Put it in a coroutine (not spawn, it is deprecated).

i dont see a while true loop here?

In your OP you had one. You should fix that because that is what people typically see.

1 Like

i dont know how to use coroutine, but is it like this?: (it probably isnt, and it still doesnt work)

local runService = game:GetService("RunService")
local players = game:GetService("Players")
local character = players.LocalPlayer.Character
repeat runService.Heartbeat:Wait() until players.LocalPlayer
local humanoid = character.Humanoid

	coroutine.yield()
hp = game.Players.LocalPlayer.Character.Humanoid.Health/100
script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.In,Enum.EasingStyle.Bounce,0.15)
runService.Heartbeat:Wait()

local TweenService = game:GetService("TweenService")
local HealthBar = players.LocalPlayer.PlayerGui.Health.HealthFrame.HealthBar
local HealthBackground = players.LocalPlayer.PlayerGui.Health.HealthFrame
HealthBar.Transparency = HealthBar.Transparency

local tweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.In, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tween = TweenService:Create(HealthBar, tweenInfo, {Transparency = 0})

HealthBackground.Transparency = HealthBackground.Transparency

local tweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.In, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tween2 = TweenService:Create(HealthBackground, tweenInfo, {Transparency = 0})
humanoid.HealthChanged:Connect(function(health)
if health <= 99 then
	--tween:Play()
	--tween2:Play()
	print("health is about full")
	game.StarterGui.Health.HealthFrame.Visible = true
	HealthBar.Visible = true
	HealthBackground.Visible = true
else
	game.StarterGui.Health.HealthFrame.Visible = false
	HealthBar.Visible = false
	HealthBackground.Visible = false
	print("healh is less than full")
	end
end)

or like this?:

coroutine.wrap(function()
local hp = game.Players.LocalPlayer.Character.Humanoid.Health/100
script.Parent:TweenSize(UDim2.new(hp,0,1,0),Enum.EasingDirection.In,Enum.EasingStyle.Bounce,0.15)
runService.Heartbeat:Wait()
end)()

after all of this, i solved it (somewhat) the main problem was (i was right) the if statement

That second one, except you need to add a while true loop in the coroutine.

1 Like