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

Why do I get this error? (Billboardgui script)

local player = game.Players.LocalPlayer
local character = player.Character
 
local bg = Instance.new("BillboardGui")
bg.Parent = player.PlayerGui
bg.Adornee = character.Head
bg.Active = true
bg.AlwaysOnTop = true
bg.Size = UDim2.new(1, 0, 1, 0)
bg.StudsOffset = Vector3.new(0, 2, 0)
local bgcopy = bg:Clone()
bgcopy.Parent = character.Head
 
local frame = Instance.new("Frame")
frame.Parent = bg
frame.Size = UDim2.new(1, 0, 1, 0)
frame.BackgroundTransparency = 1
frame.BackgroundColor3 = Color3.new(1, 1, 1)
frame.Active = true
local framecopy = frame:Clone()
framecopy.Parent = character.Head.BillboardGui

 
local text = Instance.new("TextLabel")
text.Parent = frame
text.Position = UDim2.new(0.25, 0, 0.25, 0)
text.Size = UDim2.new(.5, 0, .5, 0)
text.BackgroundTransparency = 1
text.Text = (player.NamePl.Value)
text.FontSize = "Size24"
text.TextColor3 = Color3.new(255, 255,255)
text.Active = true
local textcopy = text:Clone()
textcopy.Parent = character.Head.BillboardGui.Frame

I keep getting this error

Players.Player1.PlayerGui.LocalScript:6: attempt to index local 'character' (a nil value)

-I'm new to this, fyi.

2 answers

✓ Accepted answer

Most likely it is caused by this script running before the Character loads (hence player.Character is nil and from there is your error). However, SlickPwner's solution is incorrect as Character is not a child, but a property, of a Player object.

Unfortunately I don't think there's a one-line solution to this, but here is what we can do:

local player = game.Players.LocalPlayer
while not player.Character do
	wait()
end
local character = player.Character

We just wait for player.Character to not be nil (note that this solution doesn't work for children since accessing them would cause an error; since Character is a property accessing player.Character when the property is nil triggers no error)

4 comments

0

Similarly, you could use repeat wait() until player.Character end

0

THANK YOU, IT WORKED

0

No end. That's true, though I find repeat loops to be not as elegant since they require execution at least once (although at least in this case that is of minimal significance). The only difference is the removal of not; the not in my opinion explains the purpose of the body (waiting) better

0

Okay, thank you BlueTaslem, I will keep that in mind.

I would assume because you're not waiting for the Character. Try this:

local character = player:WaitForChild("Character")

3 comments

0

I am getting no error now, but the BillboardGui on my head isn't showing up.

0

The Character is not a child of the player. This will wait forever.

0

Oh, so what do I do now?

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