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

For some reason I keep getting an error online with Gui's can someone help?

Asked by
Damo999 182
8 years ago

I have been been messing around with Gui's and I went onto the wiki and followed a tutorial, the code works fine in studio but it' messes up online. I get the error saying "Avatar not a valid member of Frame(unitframe)" I'm not sure what the cause is .

and here is the code

local player = game.Players.LocalPlayer
local unitFrame = script.Parent


unitFrame.Avatar.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&username=" .. player.Name
unitFrame.playerName.Text = player.Name

local healthBar = unitFrame.healthBarContainer.HealthBar

player.CharacterAdded:connect(function(character)
    local humanoid = character:WaitForChild('Humanoid')
    humanoid.HealthChanged:connect(function(health)
        local healthPercentage = health / character.Humanoid.MaxHealth
        healthBar.Size = UDim2.new(healthPercentage, 0, 1, 0)
    end)
end)

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


-- hierarchy

VStarterGui
    VScreenGUI
        VunitFrame
            LocalScript
            HealthBar
            Avatar
            playerName
0
So are you sure all of the parents are in the correct places? Is avatar an image label or image button? Acheo 230 — 8y
0
avatar is an image label and I believe everything is in the right place unless roblox wiki messed in someway. Damo999 182 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

Assuming your hierarchy is correct, you should try using WaitForChild()so you're not calling upon a GUI that has not yet been loaded.

The bellow code is the same code you have, but with WaitForChild() inside of possibly needed areas.

local player = game.Players.LocalPlayer
local unitFrame = script.Parent


unitFrame:WaitForChild("Avatar").Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&username=" .. player.Name
unitFrame.playerName.Text = player.Name

local healthBar = unitFrame:WaitForChild("healthBarContainer"):WaitForChild("HealthBar")

player.CharacterAdded:connect(function(character)
    local humanoid = character:WaitForChild('Humanoid')
    humanoid.HealthChanged:connect(function(health)
        local healthPercentage = health / character.Humanoid.MaxHealth
        healthBar.Size = UDim2.new(healthPercentage, 0, 1, 0)
    end)
end)

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

Comment any questions or problems if you have any. Otherwise, if this helped then toss an Accept my way

0
Thanks @alphawolvess it works :-), now I know I should start using waitforchild now. Damo999 182 — 8y
0
FYI, you only need to use :WaitForChild() one time for an instance. It will yield the script until whatever it is waiting for exists, after it exists, you will not need to use WaitForChild() the next time you call on the instance. alphawolvess 1784 — 8y
Ad

Answer this question