Cannot change MeshPart's color according to Player's health

So, I made a “health circle” around the player that changes color when the player has a certain health value.
The first part of the script that I used to weld it to the player does work correctly.
The issue is when changing the MeshPart’s color, my game just freezes for a while, and the color doesn’t changes.

Anybody has a fix? And is there a way I can make the color transition smoother like the real heatlh bar GUI?

wait(1)
-- Services
local Players = game:GetService("Players")

-- Variables
local char = Players.LocalPlayer.Character
local humRP = char.HumanoidRootPart
local health = char.Humanoid.Health

-- Script
-- Weld
local circleThingy = script:WaitForChild("CircleThingy"):Clone()
circleThingy.Parent = char
circleThingy.CFrame = humRP.CFrame

local weld = Instance.new("ManualWeld")
weld.Part0 = humRP
weld.Part1 = circleThingy
weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame * CFrame.new(0,-3,0))
weld.Parent = weld.Part0

-- Color
while health ~= 0 do
	if health == 100 then
		circleThingy.Color = Color3.new(0, 255, 0)
	elseif health < 55 then
		circleThingy.Color = Color3.new(255, 170, 0)
	elseif health < 25 then
		circleThingy.Color = Color3.new(255, 0, 0)
	end
end

(And I want it so when the player the circleThingy turns black, that’s why its original color is black)
(And I moved the circleThingy to workspace just for the screen, it’s in a localscript located in SPS)

The while statement does not have a wait.
Instead do

while true do task.wait()
   ...
end

Plus you do not update the health so it should be:

while true do task.wait()
   health = char.Humanoid.Health
   ...
end

Now to make it smoother and not use a while loop use:

local humanoid = char.Humanoid

local function updateColor()
   ...
end

humanoid:GetPropertyChangedSignal("Health"):Connect(updateColor)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(updateColor)

Oh yeah instead of having the if statements like so:

You can use:

circleThingy.Color = Color3.fromHSV(.4 * (humanoid.Health/humanoid.MaxHealth), 1, 1)

Read this post on how it works:

I would try :GetPropertyChangedSignal() instead of using a while loop. Like this:

char.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	if health == 100 then
		circleThingy.Color = Color3.new(0, 255, 0)
	elseif health < 55 then
		circleThingy.Color = Color3.new(255, 170, 0)
	elseif health < 25 then
		circleThingy.Color = Color3.new(255, 0, 0)
	end
end)

I’ve used your method, that’s how the “-- Color” part of my script now looks:

local humanoid = char.Humanoid
local function updateColor()
	circleThingy.Color = Color3.fromHSV(.4 * (humanoid.Health/humanoid.MaxHealth), 1, 1)
end

humanoid:GetPropertySignalChanged("Health"):Connect(updateColor)
humanoid:GetPropertySignalChanged("MaxHealth"):Connect(updateColor)

But now I get an error saying: GetPropertySignalChanged is not a valid member of Humanoid “Workspace.RSmellys.Humanoid”

It isn’t working because it’s GetPropertyChangedSignal and not GetProperySignalChanged

Replace the two lines at the bottom with this:

humanoid:GetPropertyChangedSignal("Health"):Connect(updateColor)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(updateColor)

If it works, please mark the other person’s post as solution! I didn’t write their code and don’t want credit for it!

1 Like

I fixed the code, you can copy paste it now and it’ll be correct!

Yep, it fixed the code, but when I respawn the circleThingy isn’t here anymore, should I use a while do to that?

I would add the code into a player.CharacterAdded event


local localPlayer = Players.LocalPlayer

localPlayer.CharacterAdded:Connect(function()
    local circleThingy = script:WaitForChild("CircleThingy"):Clone()
    circleThingy.Parent = char
    circleThingy.CFrame = humRP.CFrame
    
    local weld = Instance.new("ManualWeld")
    weld.Part0 = humRP
    weld.Part1 = circleThingy
    weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame * 
    CFrame.new(0,-3,0))
    weld.Parent = weld.Part0
end)

When you respawn it does not appear because the script is put inside PlayerStarterScripts, instead put it in CharacterStarterScripts for a quick fix.

Else you’d need to use

local LocalPlayer = game:GetService("Players").LocalPlayer

local function characterAdded (char)
   ... -- all previous code
end

-- if character already loaded before the script loaded
if LocalPlayer.Character then characterAdded(LocalPlayer.Character) end 
-- every time the character respawns:
game:GetService("Players").LocalPlayer.CharacterAdded:Connect(charAdded)

and put the code in there, this would work as well.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.