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

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

Asked by 9 years ago

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

0
Sometimes I find it takes just a bit to load the assets of a GUI. randomsmileyface 375 — 9y
0
Have you tried replacing the line unitFrame.Avatar with unitFrame:WaitForChild("Avatar")? randomsmileyface 375 — 9y
0
It works! Thank you so much! teafulfireball 0 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

Is Avatar Inside The Frame?

0
Yes, the frame is called UniteFrame teafulfireball 0 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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.

Answer this question