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
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)
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.
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:
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
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()
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.
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.
----// Script //-----
local humanoid = game:FindFirstChild(“CoastalBaseHealthPart”)
local textLabel = script.Parent – changed to fix the textLabel location
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.
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
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().