Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Custom Health Script help?

Asked by 8 years ago

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?
0
also sorry for it being all stuffed up together,im kinda a noob dracomaster153 15 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

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.

0
still not working but thanks for trying dracomaster153 15 — 8y
0
@dracomaster153 Could you explain what part of the script isn't working? If there's an error, comment with the error that's occurring. Programmix 285 — 8y
0
well for some reason,its not moving, when i go to a spike or something,the health bar doesnt moved,and go down as you take damage.So i think its the script dracomaster153 15 — 8y
0
@dracomaster153 Do any errors appear in the Output? Programmix 285 — 8y
View all comments (6 more)
0
O.O well it says 22:34:17.227 - Script 'Players.Player.PlayerGui.Health.LocalScript', Line 6 22:34:17.227 - Stack End but one thing that might be the problem the thing it says is: Frame is not a valid member of screengui.So can you plz tell me what they mean by that? dracomaster153 15 — 8y
0
ill ask another person bout it dracomaster153 15 — 8y
0
@dracomaster153 What it means is that Lua can't find an object called "Frame" inside of "ScreenGui". Make sure that you're properly referencing objects Programmix 285 — 8y
0
Still not working. :( dracomaster153 15 — 8y
0
@dracomaster153 I helped to the best of my ability with the resources you've shown me. Could you take a picture of your entire StarterGui and link it here? Programmix 285 — 8y
0
WELL I DONT ANYMORE CUZ I GOT IT,i finally got what you said so thx dracomaster153 15 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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.

Answer this question