Wrap your code
When posting code, use code block to wrap your code.
To do that, select your code with mouse and press the blue lua button when posting the question.
A better way to get character
local char = workspace:findFirstChild(script.Parent.Parent.Parent.Name)
Here, you are getting character by player's name.
While this will work (Except if there is something in Workspace that is named the same as player),
I would advise you to use this:
local char = script.Parent.Parent.Parent.Character
Update only if needed
You are also updating the gui no matter what changed in the Humanoid, which is unnecessary.
You should check if changed property is Health (also MaxHealth if max health can be changed in the game)
hum.Changed:connect(function(property)
if property == "Health" then
end
end)
Always check your tree if you referenced your objects correctly
Make sure that there is a gui object named 'Text' that is a child of Frame.
script.Parent.Frame.Text.TextLabel.Text = ""..hum.MaxHealth.."/"
Based on your code, I would say this is what you wanted unless 'Text' really is a gui object parented to Frame.
And, you can't concatenate a string and number, so you should use tostring function.
script.Parent.Frame.Health.TextLabel.Text = ""..tostring(hum.MaxHealth).."/"
Make sure your logic is correct
Also, this code part seems not to do what you might have intended
if hum.Health <= hum.MaxHealth*0.90 then
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(150,0,0)
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(0,170,0)
end
You should add an else statement
if hum.Health <= hum.MaxHealth*0.90 then
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(150,0,0)
else
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(0,170,0)
end
Check if there is something else making it not work
And finally what does HP script do?
It might be interfering with your script.
The full code
Adding fixes that @Programmix made, here is the full code.
local char = script.Parent.Parent.Parent.Character
local hum = char.Humanoid
hum.Changed:connect(function(property)
if property == "Health" then
script.Parent.Frame.Health.Frame.Size = UDim2.new(hum.Health/hum.MaxHealth,0,1,0)
script.Parent.Frame.Health.TextLabel.Text = tostring(hum.Health).."/" ..tostring(hum.MaxHealth)
if hum.Health <= hum.MaxHealth*0.90 then
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(150/255,0,0)
else
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(0,170/255,0)
end
end
end)
wait(1)
script.Parent.HP.Disabled = false
We can't read your mind
Make your questions clear, give us any errors that occurred.
It's hard to help when you don't give us a tree hierarchy of your gui.
Also, I am not sure exactly what you want to achieve at some points of your code.
I have assumed some things and I hope I understood you correctly.
I hope this helped.
1 comment
also sorry for it being all stuffed up together,im kinda a noob