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

Why wont this mana and health bar script work?

Asked by
Scrxptss -13
7 years ago
local plr = game.Players.LocalPlayer
local pmana = plr.Mana
local Humanoid = plr.Character:WaitForChild("Humanoid")
local health = script.Parent.Health
local healthShade = health.Shade
local mana = script.Parent.Mana
local manaShade = mana.Shade
local maxHealth = Humanoid.MaxHealth
local phealth = Humanoid.Health
wait(2)

function HealthChange()
    health.Size = UDim2.new(0,phealth/maxHealth,0,30)
    healthShade.Size = UDim2.new(0,phealth/maxHealth,0,2)
end

function ManaChange()
    mana.Size = UDim2.new(pmana/100,0,0,30)
    manaShade.Size = UDim2.new(pmana/100,0,0,2)
    repeat
        wait(0.5)
        mana.Value = mana.Value + math.random(1,3)
    until mana.Value == 100
end

Humanoid.Changed:connect(HealthChange)
pmana.Value.Changed:connect(ManaChange)

When the health changes the bar moves it just doesnt move the right way. Mana does not work at all.

1 answer

Log in to vote
0
Answered by
Async_io 908 Moderation Voter
7 years ago
local plr = game.Players.LocalPlayer
local pmana = plr:WaitForChild('Mana') --Wait for these as they won't load immediately.
plr.CharacterAdded:connect(function(character) --Will wait till the character is loaded
    local Humanoid = plr.Character:WaitForChild("Humanoid")
    local health = script.Parent.Health --Wait for these as they won't load immediately.
    local healthShade = health.Shade
    local mana = script.Parent.Mana
    local manaShade = mana.Shade
    local maxHealth = Humanoid.MaxHealth
    local phealth = Humanoid.Health

    Humanoid.HealthChanged:connect(function() --Health changed function =)
        health.Size = UDim2.new(phealth/maxHealth,0,0,30) --Switched the 0 and health math
        healthShade.Size = UDim2.new(phealth/maxHealth,0,0,2)  --Switched the 0 and health math
    --If you want to make this move smoothly, you could use TweenSize 
    end)

    pmana.Changed:connect(function() --Just used the value's name and .changed.
        mana.Size = UDim2.new(pmana/100,0,0,30)
        manaShade.Size = UDim2.new(pmana/100,0,0,2)
       --[[ repeat
            wait(0.5)
            mana.Value = mana.Value + math.random(1,3)
        until mana.Value == 100]]-- --Use this else where.
    --If you want to make this move smoothly, you could use TweenSize 
    end)
end)
Ad

Answer this question