Anyone knows whats wrong with my custom health gui here my script:
local char = workspace:findFirstChild(script.Parent.Parent.Parent.Name) hum = char.Humanoid hum.Changed:Connect(function() script.Parent.Frame.Health.Frame.Size =UDim2.new(hum.Health/hum.MaxHealth,0,1,0) script.Parent.Frame.Text.TextLabel.Text = ""..hum.MaxHealth.."/" 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 end) also heres my second script thats connected to the first one: wait(1) script.Parent.HP.Disabled = false anyone can tell meh whats wrong?
Next time, be sure to put your code in a code block.
This is a fixed version of your script.
[LocalScript]
local player = game.Players.LocalPlayer local char = player.Character local hum = char.Humanoid hum.Changed:connect(function() script.Parent.Frame.Health.Frame.Size = UDim2.new(hum.Health/hum.MaxHealth,0,1,0) script.Parent.Frame.Text.TextLabel.Text = hum.Health .."/" .. hum.MaxHealth if hum.Health <= hum.MaxHealth*0.90 then script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(150/255,0,0) script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(0,170/255,0) end end)
Here's what changed.
First, we changed this line...
hum = char.Humanoid
... to this:
local hum = char.Humanoid
It's always good practice to make your variables local
.
Next, we changed this line...
script.Parent.Frame.Text.TextLabel.Text = ""..hum.MaxHealth.."/"
... to this:
script.Parent.Frame.Text.TextLabel.Text = hum.Health .."/" .. hum.MaxHealth
Since I think that's what you meant to do. If it isn't, you can revert the change.
I also changed these lines...
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(150,0,0) script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(0,170,0)
... to this:
script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(150/255,0,0) script.Parent.Frame.Health.Frame.BackgroundColor3 = Color3.new(0,170/255,0)
The main issue with what you were doing before is that you were supplying Color3.new
with raw numbers. Color3 actually takes a decimal value from 0 to 1, so make sure you divide the number by 255.
I hope this helps you out. Good luck! Let me know if you have any more questions or if this didn't solve your problem.
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 -- code 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.