Trouble displaying part health on a text label

Hello! What I am trying to do is simply display the health of a part in the workspace (it is in several folders) with a text label in startergui. I basically want to display the health of the part as it will act as the health indicator for a base.

I have used several lua script generators as I am obviously not a scripter. However there are no errors showing up and it is obviously not working.

I have tried researching where I could do this however I could not find any resources that were able to help me.

---- Script (In the textlabel) ----
local BaseHealthPart = game.Workspace.Kingdoms[“Coastal Colony”].BaseHealthPart

local textLabel = script.Parent:FindFirstChild(“CoastalHealthLabel”)

if BaseHealthPart and textLabel then
textLabel.Text = "Health: " … tostring(BaseHealthPart.Health)
end

1 Like

Firstly, it seems the location to the textLabel was incorrect (looking in the wrong directory).
Secondly, I made it a changed event so that it should update automatically.

local BaseHealthPart = game.Workspace.Kingdoms[“Coastal Colony”].BaseHealthPart
local textLabel = script.Parent -- changed to fix the textLabel location

BaseHealthPart:GetAttributeChangedSignal("Health"):connect(function()
	textLabel.Text = "Health: ".. tostring(BaseHealthPart.Health)
end)
1 Like

I dont know what the “BaseHealthPart” is but if its a int/number value then use tostring(BaseHealthPart.Health.Value)

Hey! I tried this script and it didn’t seem to work. So because of that I should probably clarify where everything is at, and how they are arranged. Keep in mind I did rename a few things. But I basically have the workspace stuff, the gui stuff, and how the text label(s) are arranged on the screen (The one on the top left is the CoastalHealthLabel). Hopefully this helps.

Screenshot (7)
Screenshot (8)
Screenshot (5)

Hey, think I know why it wont work. In your script the textlabel variable is this:

script.Parent:FindFirstChild(“CoastalHealthLabel”)

However because the script IS inside the textlabel that means the “FindFirstChild(“CoastalHealthLabel”)” is just trying to find a instance within the textlabel. Use this as the variable instead:

local textLabel = script.Parent

Tell me if this helps!

Hey! So I did try this after @JayzYeah32 pointed it out to me however for some reason it still wont display the parts health. If you need me to and if you think its worth the trouble, I could possibly share the game with either of you.

---- Script ----
local BaseHealthPart = game.Workspace.Kingdoms.CoastalColony.CoastalBaseHealthPart.BaseHealth
local textLabel = script.Parent – changed to fix the textLabel location

BaseHealthPart:GetAttributeChangedSignal(“Health”):connect(function()
textLabel.Text = "Health: "… tostring(BaseHealthPart.Health)
end)

Didnt realize he said that, I can’t try to replicate the script right now but i noticed in the “textLabel.Text” area you put 3 dots. Try putting 2 dots and if that does help try to use print()

1 Like
local plr = game.Players.LocalPlayer

local healthLabel = plr.PlayerGui.ScreenGui.health

local part = workspace.Part
local hisHealth = part:FindFirstChildWhichIsA("IntValue")

healthLabel.Text = "Health: "..hisHealth.Value

hisHealth.Changed:Connect(function()
	healthLabel.Text = "Health: "..hisHealth.Value
end)

Change the humanoid to an int, or keep it humanoid

That’s a good idea, but I want this part to act as a damage taker in a way. When a player hits the part it subtracts health from the part/humanoid then reduces the health on the UI.

easy! Just do part.Int.Value -= damage (or what ever you call the int value)

It is likely because you are using a script and not a localscript in the players GUI.
A script runs on the server whereas a localscript is intended to run on the client.

Switching the script for a localscript should do the trick.

local humanoid = game:GetService("Workspace").Kingdoms.CoastalColony.CoastalBaseHealthPart.BaseHealth -- fixed the path location
local textLabel = script.Parent

humanoid:GetAttributeChangedSignal("Health"):connect(function()
	textLabel.Text = "Health: ".. tostring(humanoid.Health)
end)

Hey! So, I think we’re getting somewhere. There is an actual error this time, I’m not entirely sure why it is referencing a model? Should I simplify where everything is at? But at least is is recognizing the parts this time.

Try using :WaitForChild("CoastalBaseHealthPart") instead

Hey! So I tried this as well and I got an error and wondered if you understood what it meant.

Error:

----// Script //-----
local humanoid = game:FindFirstChild(“CoastalBaseHealthPart”)
local textLabel = script.Parent – changed to fix the textLabel location

humanoid:GetAttributeChangedSignal(“Health”):connect(function()
textLabel.Text = "Health: "… tostring(humanoid.Health)
end)

You’re main problem here is thinking AI can make up for research.
First few steps to new things, should be looking for documentation, examples and tutorials.
AI has a way of skipping nuances that come back to haunt you later…
in turn it ends up being copying and not learning.

How to make a Health Bar Gui in Roblox Studio ← tutorial
Simple Health Bar GUI ← toolbox premade you can reverse engineer
Working with these will gain skills over copying AI.

First issue is that the humanoid variable is wrong, not sure if that’s a mistake but yeah. Anyways try using GetPropertyChangedSignal.

Should look something like this:

humanoid:GetPropertyChangedSignal("Health"):connect(function()
	textLabel.Text = "Health: " .. tostring(humanoid.Health)
end)

Pretty sure the problem was that you were using GetAttributeChangedSignal and not GetPropertyChangedSignal.

2 Likes

So, I have tried that and it shows the same error for that as well. I have also tried this script but again said the same thing.

----// Script //----
local humanoid = game:FindFirstChild(“CoastalBaseHealthPart”)
local textLabel = script.Parent – changed to fix the textLabel location

humanoid:FindFirstChild(“CoastalBaseHealth”).Health:connect(function()
textLabel.Text = "Health: "… tostring(humanoid.Health)
end)

Not sure if you changed where the humanoid is located but try this:

local humanoid = workspace.Kingdoms.CoastalColony.CoastalBaseHealthPart.BaseHealth
local textLabel = script.Parent

humanoid:GetPropertyChangedSignal("Health"):connect(function()
	textLabel.Text = "Health: ".. tostring(humanoid.Health)
end)

If you changed the humanoid to be somewhere else then its most likely because you’re using game:FindFirstChild(). Assuming the humanoid is in workspace try using workspace:FindFirstChild().