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

How do i stop my healthbar gui from going off the screen?

Asked by 3 years ago

This is my healthbar's script

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local healthbar = script.Parent.Frame:WaitForChild("HealthBar")

game:GetService("RunService").RenderStepped:Connect(function()
    local humanoid = char:FindFirstChild("Humanoid")
    healthbar.Health.Size = UDim2.new(humanoid.Health/humanoid.MaxHealth,0,1,0)
end)

When you load in everything is fine but when the player gains more health the gui goes off the screen and the only way i found to fix this is by resetting. When you reset the gui works perfectly.

So what do i have to do to make the gui work as the player gains strength without having to reset

3 answers

Log in to vote
0
Answered by
jundell 106
3 years ago
Edited 3 years ago

First off, you realllyyy shouldn’t use RenderStepped for this. RenderStepped runs every single frame, which seems a little unnecessary. Instead of RenderStepped, detect the humanoid’s health change. What seems to be your problem is the Humanoid’s health being greater than their MaxHealth (which, may not be your problem since I can’t debug the script right now) because I don’t see anything else wrong here. To fix this, just add a simple check.

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local healthbar = script.Parent.Frame:WaitForChild("HealthBar")

humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    if humanoid.Health > humanoid.MaxHealth then
        healthbar.Health.Size = UDim2.new(humanoid.MaxHealth/humanoid.MaxHealth,0,1,0)
        return
    end
    healthbar.Health.Size = UDim2.new(humanoid.Health/humanoid.MaxHealth,0,1,0)
end)
0
instead of using an if statement i would just use math.min(). Benbebop 1049 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I think i kinda figured it out. When a player joined their current health was higher than their max health so the gui was not updated. Now I'm thinking there's something wrong with my health script and im not sure how to fix it.

This is my health script, basically its suppose to give you more health based on your strength value. For example someone with 100 strength will have more health than someone with 20 strength, etc.

local DefaultHealth = 100

game.Players.PlayerAdded:Connect(function(player)
    local Stats = player:WaitForChild("leaderstats")
    local Strength = Stats:FindFirstChild("strength")
    local char = player.Character or player.CharacterAdded:wait()
    if char then
        local hum = char:FindFirstChild("Humanoid")
        if hum then
            hum.MaxHealth = DefaultHealth * Strength.Value * 1.25
        end

        Strength.Changed:connect(function(value)
            if player.Character and player.Character:FindFirstChild("Humanoid") then
                player.Character.Humanoid.MaxHealth = DefaultHealth * Strength.Value * 1.25
            end
        end)
    end
end)
0
“When a player joined their current health was higher than their max health so the gui was not updated.” My answer should solve this problem. jundell 106 — 3y
0
Whenever you change the MaxHealth, you should also change the Health to the updated MaxHealth so the player doesn’t have to regen their health. jundell 106 — 3y
0
i used your script and the problem was the same, well it seemed to fix it but whenever the player resets their health goes to 100 and one of the player tools not working. Ik it has to deal with the health script above but im not sure what to do ItzSulfonic 61 — 3y
Log in to vote
0
Answered by 3 years ago

You can try it with 'if'

Put into the function

if Humanoid.Health <= 0 then --if the health from player = 0 then health bar will be resize
    healthbar.Health.Size = UDim2.new("0,0,1,0) --Set the Bar to end and not out of screen.
end

I think that should work :-D

Answer this question