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
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