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

Why do I get this error? (Billboardgui script)

Asked by 9 years ago
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

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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)

0
Similarly, you could use repeat wait() until player.Character end SlickPwner 534 — 9y
0
THANK YOU, IT WORKED PancakeChop 35 — 9y
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 BlueTaslem 18071 — 9y
0
Okay, thank you BlueTaslem, I will keep that in mind. PancakeChop 35 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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

local character = player:WaitForChild("Character")
0
I am getting no error now, but the BillboardGui on my head isn't showing up. PancakeChop 35 — 9y
0
The Character is not a child of the player. This will wait forever. BlueTaslem 18071 — 9y
0
Oh, so what do I do now? PancakeChop 35 — 9y

Answer this question