so I have to ask this question a second time since nobody answered my last one
so I made a health GUI and it works perfectly fine but there is one slight problem it wont regenerate every 3 seconds like add 1 health every 3 seconds or something and it still wont work
wait(2) game.Players.LocalPlayer.Character.Humanoid.Changed:connect(function() local redzone = game.Players.LocalPlayer.Character.Humanoid.MaxHealth / 4 local yellowzone = game.Players.LocalPlayer.Character.Humanoid.MaxHealth / 2 if game.Players.LocalPlayer.Character.Humanoid.Health <= yellowzone then script.Parent.BackgroundColor3 = Color3.new(255,255,0) end if game.Players.LocalPlayer.Character.Humanoid.Health > yellowzone then local brickcolor = BrickColor.new("Lime green") local color = brickcolor.Color script.Parent.BackgroundColor3 = color end if game.Players.LocalPlayer.Character.Humanoid.Health <= redzone then script.Parent.BackgroundColor3 = Color3.new(255,0,0) end if game.Players.LocalPlayer.Character.Humanoid.Health > redzone then local brickcolor = BrickColor.new("Lime green") local color = brickcolor.Color script.Parent.BackgroundColor3 = color end script.Parent.Size = UDim2.new(game.Players.LocalPlayer.Character.Humanoid.Health / game.Players.LocalPlayer.Character.Humanoid.MaxHealth,0,0,20) end)
Hello, please make sure to indent your code, you also should create variables as they make things a lot easier and really prevents some issues from happening with your code, It seems also that the code:
if game.Players.LocalPlayer.Character.Humanoid.Health > yellowzone then local brickcolor = BrickColor.new("Lime green") local color = brickcolor.Color script.Parent.BackgroundColor3 = color end
is not needed as if Humanoid.Health is already higher than the redzone It'll already change to Lime green.
I've modified your code in a way that it works with the GUI I've tested which should work for yours too, Once again I suggest to make variables as they're not evil and they're also really helpful!
In order to regenerate the player's health you'll need a remoteevent since you cannot do it from a localscript, in this case I created a remoteevent called " RegenHealth " in workspace which will be used to regenerate the player's health.
Modified localscript:
-- Try to put variables as they're really helpful! local LocalPlayer = game:GetService("Players").LocalPlayer local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:wait() local Humanoid = Character:WaitForChild("Humanoid") local RegenEvent = game.workspace.RegenHealth Humanoid.Changed:Connect(function() local redzone = (Humanoid.MaxHealth / 4) local yellowzone = (Humanoid.MaxHealth /2) if Humanoid.Health <= redzone then -- If Humanoid.Health is lower or equal than redzone script.Parent.BackgroundColor3 = Color3.new(255,0,0) end if Humanoid.Health > redzone then -- If Humanoid.Health is higher than redzone local color = BrickColor.new("Lime green").Color script.Parent.BackgroundColor3 = color end script.Parent.Size = UDim2.new(Humanoid.Health / Humanoid.MaxHealth,0,0,20) -- Changes the size of the gui with the Health and MaxHealth the player currently has. end) while wait(1) do -- Change 1 to the seconds you want it to regenerate RegenEvent:FireServer(1) -- Change the 1 to how much you want it to regenerate end
ServerScript ( Inside remoteevent )
script.Parent.OnServerEvent:Connect(function(Player, HealthAmount) local Character = Player.Character or Player.CharacterAdded:Wait() local Humanoid = Character:WaitForChild("Humanoid") Humanoid.Health = Humanoid.Health + HealthAmount end)
Hopefully this solves your issue! Make sure to accept the answer if it did.