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

HP script isnt working (a gui health bar)?

Asked by 7 years ago
Edited 7 years ago
function _G.round(n)
return n - math.floor(n) >= 0.5 and math.ceil(n) or n - math.floor(n) < 0.5 and math.floor(n)
end

wait(0.5)
local char = script.Parent.Parent.Parent.Parent.Character
hum = char.Humanoid
Digit = script.Parent.Health.Digits
Round = (_G.round(hum.Health))
hum.Changed:connect(function()
script.Parent.Health.Frame.Size = UDim2.new(hum.Health/hum.MaxHealth,0,1,0)
Round = (_G.round(hum.Health))
RoundTwo = (_G.round(hum.MaxHealth))
Digit.Text = (Round.." / "..RoundTwo.." HP")
if hum.Health <= hum.MaxHealth*0.20 then
script.Parent.Health.Frame.BackgroundColor3 = Color3.new(255,0,0)
else
script.Parent.Health.Frame.BackgroundColor3 = Color3.new(0,255,0)
end
end)

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

First, for us to help you, we need to know what isn't working. Make sure you have the Output window open and read what the error is (if any) - it will be in red. If no error appears when you run the place, we need to know that too. In either case, you should let us know what is and isn't working/occurring (being as specific as you can). Don't forget that you can add print statements and they will show up in the Output, allowing you to get a sense of what you're script is and isn't doing (and perhaps why).

This script is a bit of a mess. Tips:

  • Generally, you probably shouldn't use "_G". Use module scripts to share functions, classes, or even values between scripts. You're less likely to accidentally overwrite something (as you might if you use _G), and it makes organization easier.
  • Use tabs to indent code
  • You could probably replace the wait(0.5) with wait(). Ideally you don't need to use either, but it can solve some problems.
  • Round can be defined like this (and will return the same value as your version, with less code):
local function round(n)
    return math.floor(n+0.5)
end
  • I recommend against having script.Parent.Parent.Parent.Parent.Character in the code (that's a lot of .Parent's, and it isn't easy for anyone (including you!) to tell if it's correct); use game.Players.LocalPlayer.Character instead.
  • On line 7, you assume that the Humanoid has loaded. Use :WaitForChild("Humanoid") instead.
  • Don't assign to a variable if you aren't going to use its value (ex you don't use the value assigned to Round on line 9)
  • You refer to Health.Frame multiple times. Assign it to a local variable and it'll be easier to follow
  • You don't need parentheses around an expression (lines 9, 12, 13, 14).
  • Use a consistent naming style (you have some variables capitalized and others not).
  • When the humanoid dies, the entire character model is replaced with a new one. If you have set StarterGui.ResetPlayerGuiOnSpawn to false, you will need to update the variable 'hum' and re-establish the .Changed event (and maybe disconnect the old .Changed event)

Improved script:

local function round(n)
    return math.floor(n+0.5)
end

wait()
local player = game.Players.LocalPlayer
local hum = player.Character.char:WaitForChild("Humanoid")
local health = script.Parent.Health
local digit = health.Digits
local frame = health.Frame
hum.Changed:connect(function()
    frame.Size = UDim2.new(hum.Health/hum.MaxHealth,0,1,0)
    Digit.Text = round(hum.Health) .. " / " .. round(hum.MaxHealth) .. " HP"
    if hum.Health <= hum.MaxHealth * 0.2 then
        frame.BackgroundColor3 = Color3.new(255, 0, 0)
    else
        frame.BackgroundColor3 = Color3.new(0, 255, 0)
    end
end)

Now, if it wasn't working before, it probably won't be working now (unless it wasn't working because you had the wrong number of .Parents), but at least it's easier to read what's going on.

The next step in debugging, after checking the Output window for errors, is to print out what is going on. ex, after the hum.Changed lined you might put a print("Humanoid changed health") to confirm that it is indeed running. Then you could print out hum.Health, MaxHealth, and any other variables/values you want.

Ad

Answer this question