Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

There's an error in my script that I got off from Roblox Wiki, how can I fix it? (Please help!)

So I've been learning from Roblox Wiki, and on one of the lessons is called "Making a Unit Frame" which shows you how to code it, but when I created it and joined my game, it doesn't show my username or my avatar, so I checked out the Developer Console and it says "Avatar not a valid member of frame" Here is the code I got from the Wiki:

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)

And here's the picture from the Explorer: http://prntscr.com/8ok1u9

3 comments

0

Sometimes I find it takes just a bit to load the assets of a GUI.

0

Have you tried replacing the line unitFrame.Avatar with unitFrame:WaitForChild("Avatar")?

0

It works! Thank you so much!

2 answers

Try this:

local player = game.Players.LocalPlayer
local unitFrame = script.Parent.Parent:WaitForChild("UnitFrame")

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)

WaitForChild is different from just dots because sometimes thing need to load and they haven't loaded, so it is a good thing you do this.

Please accept if helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.