Why am I receiving "attempt to index nil with 'FindFirstChild'"?
Asked by
4 years ago Edited 4 years ago
I am receiving "attempt to index nil with 'FindFirstChild'" from two of my scripts, namely ChatHandler and ChangeNameScript, on account of identifying a GUI placed in the player's head by my localscript, Name title giver.
Specifically, I am getting "Workspace.ChatHandler:19: attempt to index nil with 'FindFirstChild'" and "ServerScriptService.ChangeNameScript:8: attempt to index nil with 'FindFirstChild'".
The ChatHandler and ChangeNameScript are supposed to identify the text of the nametag (NameGui.NameLabel.Text) provided by Name title giver, but it does not. The NameLabel, NameGui, and player's head exist at the time in which the scripts go to identify them, however, they don't identify them. I am at my wit's end as to why. I appreciate any response, help, or suggestion. I am new to coding and I am truly lost as to what I am doing wrong. PLEASE tell me if you need any further clarification, I will gladfully provide it. Thank you so much
Below are my scripts and screenshots of my workspace.
https://ibb.co/1Xcmfhv
https://ibb.co/w4pkWS0
Here is my ChatHandler script:
01 | local chatstorage = game.Workspace.ChatStorage |
02 | local onChatInputted = game.ReplicatedStorage:WaitForChild( "OnChatInputted" ) |
04 | onChatInputted.OnServerEvent:Connect( function (plr, msg) |
07 | local char = plr.Character or plr.CharacterAdded:Wait() |
08 | local Head = char:WaitForChild( "Head" ) |
09 | local stringval = Instance.new( 'StringValue' , chatstorage) |
10 | local success, errormsg = pcall ( function () |
11 | filteredText = game:GetService( "TextService" ):FilterStringAsync(msg, plr.UserId) |
14 | local success 2 , errormsg 2 = pcall ( function () |
15 | filteredString = filteredText:GetNonChatStringForBroadcastAsync() |
18 | local nameGui = Head:FindFirstChild( "NameGui" ) |
19 | local nameLabel = nameGui:FindFirstChild( "NameLabel" ) |
20 | stringval.Name = '[' ..plr.Team.Name.. '] ' ..nameLabel.Text.. ': ' ..string.upper(string.sub(filteredString, 1 , 1 ))..string.sub(filteredString, 2 ) |
21 | local RS = game:GetService( "ReplicatedStorage" ) |
22 | local Chat = RS:WaitForChild( "Chat" ):Clone() |
23 | local Bubble = Chat:WaitForChild( "Bubble" ) |
24 | local TextLabel = Bubble:WaitForChild( "TextLabel" ) |
25 | TextLabel.Text = string.upper(string.sub(filteredString, 1 , 1 ))..string.sub(filteredString, 2 ) |
28 | if string.len(string.upper(string.sub(filteredString, 1 , 1 ))..string.sub(filteredString, 2 ))> 15 then game.Debris:AddItem(Chat, 9 ) else game.Debris:AddItem(Chat, 4 ) end |
Here is my ChangeNameScript:
01 | local remoteEvent = game.ReplicatedStorage.NameChangeEvent |
03 | remoteEvent.OnServerEvent:Connect( function (plr, name) |
04 | local char = plr.Character or plr.CharacterAdded:Wait() |
05 | local filteredName = game:GetService( "TextService" ):FilterStringAsync(name, plr.UserId) |
06 | local filteredNameAsString = filteredName:GetNonChatStringForBroadcastAsync() |
07 | local nameGui = char.Head:FindFirstChild( "NameGui" ) |
08 | local nameLabel = nameGui:FindFirstChild( "NameLabel" ) |
09 | nameLabel.Text = filteredNameAsString |
Here is my Name title giver script:
01 | local Players = game:GetService( "Players" ) |
02 | local Player = game.Players.LocalPlayer |
04 | local Teams = game:GetService( "Teams" ) |
05 | local Character = Player.Character |
07 | function NameGiver(Player) |
08 | print ( "Name given: START" ) |
09 | local nameGui = Instance.new( "BillboardGui" , Character.Head) |
10 | nameGui.StudsOffset = Vector 3. new( 0 , 2 , 0 ) |
11 | nameGui.MaxDistance = 30 |
12 | nameGui.Size = UDim 2. new( 0 , 200 , 0 , 50 ) |
13 | nameGui.Name = "NameGui" |
14 | local nameLabel = Instance.new( "TextLabel" ) |
15 | nameLabel.Text = game.Players.LocalPlayer.Name |
16 | nameLabel.TextStrokeTransparency = 0 |
17 | nameLabel.TextScaled = true |
18 | nameLabel.Font = "Fantasy" |
19 | nameLabel.Size = UDim 2. new( 0 , 200 , 0 , 50 ) |
20 | nameLabel.BackgroundTransparency = 1 |
21 | nameLabel.TextColor 3 = game.Players.LocalPlayer.TeamColor.Color |
22 | nameLabel.Name = "NameLabel" |
23 | nameLabel.Parent = nameGui |
24 | print ( "Name given: FINISHED" ) |
28 | local function onChanged(property) |
29 | if property = = "Team" then |
35 | Player:GetPropertyChangedSignal( "Team" ):Connect(NameGiver) |
36 | Player.Changed:Connect(onChanged) |