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)
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:
wait(0.5)
with wait()
. Ideally you don't need to use either, but it can solve some problems.local function round(n) return math.floor(n+0.5) end
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.:WaitForChild("Humanoid")
instead.Health.Frame
multiple times. Assign it to a local variable and it'll be easier to followImproved 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 .Parent
s), 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.