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:
local chatstorage = game.Workspace.ChatStorage local onChatInputted = game.ReplicatedStorage:WaitForChild("OnChatInputted") onChatInputted.OnServerEvent:Connect(function(plr, msg) local filteredText local filteredString local char = plr.Character or plr.CharacterAdded:Wait() local Head = char:WaitForChild("Head") local stringval = Instance.new('StringValue', chatstorage) local success, errormsg = pcall(function() filteredText = game:GetService("TextService"):FilterStringAsync(msg, plr.UserId) end) if success then local success2, errormsg2 = pcall(function() filteredString = filteredText:GetNonChatStringForBroadcastAsync() end) if success2 then local nameGui = Head:FindFirstChild("NameGui") local nameLabel = nameGui:FindFirstChild("NameLabel") stringval.Name = '['..plr.Team.Name..'] '..nameLabel.Text..': '..string.upper(string.sub(filteredString,1,1))..string.sub(filteredString,2) local RS = game:GetService("ReplicatedStorage") local Chat = RS:WaitForChild("Chat"):Clone() local Bubble = Chat:WaitForChild("Bubble") local TextLabel = Bubble:WaitForChild("TextLabel") TextLabel.Text = string.upper(string.sub(filteredString,1,1))..string.sub(filteredString,2) Chat.Adornee = Head Chat.Parent = Head 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 else warn(errormsg2) stringval:Destroy() end else warn(errormsg) stringval:Destroy() end end)
Here is my ChangeNameScript:
local remoteEvent = game.ReplicatedStorage.NameChangeEvent remoteEvent.OnServerEvent:Connect(function(plr, name) local char = plr.Character or plr.CharacterAdded:Wait() local filteredName = game:GetService("TextService"):FilterStringAsync(name, plr.UserId) local filteredNameAsString = filteredName:GetNonChatStringForBroadcastAsync() local nameGui = char.Head:FindFirstChild("NameGui") local nameLabel = nameGui:FindFirstChild("NameLabel") nameLabel.Text = filteredNameAsString end)
Here is my Name title giver script:
local Players = game:GetService("Players") local Player = game.Players.LocalPlayer local Teams = game:GetService("Teams") local Character = Player.Character function NameGiver(Player) print("Name given: START") local nameGui = Instance.new("BillboardGui", Character.Head) nameGui.StudsOffset = Vector3.new(0, 2, 0) nameGui.MaxDistance = 30 nameGui.Size = UDim2.new(0, 200, 0, 50) nameGui.Name = "NameGui" local nameLabel = Instance.new("TextLabel") nameLabel.Text = game.Players.LocalPlayer.Name nameLabel.TextStrokeTransparency = 0 nameLabel.TextScaled = true nameLabel.Font = "Fantasy" nameLabel.Size = UDim2.new(0, 200, 0, 50) nameLabel.BackgroundTransparency = 1 nameLabel.TextColor3 = game.Players.LocalPlayer.TeamColor.Color nameLabel.Name = "NameLabel" nameLabel.Parent = nameGui print("Name given: FINISHED") end -- Manual detection of a property change local function onChanged(property) if property == "Team" then print("TEAM CHANGED") end end -- Connect both events Player:GetPropertyChangedSignal("Team"):Connect(NameGiver) Player.Changed:Connect(onChanged)
When ur getting the error "attempt to index nil with something" it means that your doing nil:Something / nil.Something, in this case you are doing nil:FindFirstChild()
On line 19 in ChatHandler script, nameGui
is nil because on the line above it didnt find it and thus FindFirstChild returned nil
On line 8 in ChangeName Script you have exactly the same issue, it didnt find the nameGui
in the line above and thus returned nil so the variable nameGui
is nil, you are doing nil:FindFirstChild()
I think it didnt find it because you added NameGui clientsided so, so technically the server cant see it, all changes made on the client doesnt replicate on the server (few execeptions)
You can easily see what the server actually sees by doing localserver test and check the server window and look through the explorer
edit: I couldnt help but notice in the first screenshot where you can see name nameGui, but when just press play here, it's like a mash up of client and server together, sometimes you can do things in that mode that wouldnt work in localserver (alteast in my experience)
Error(s): "Workspace.ChatHandler:19: attempt to index nil with 'FindFirstChild'" and ServerScriptService.ChangeNameScript:8: attempt to index nil with 'FindFirstChild'".
What the errors mean: something doesn't exist, so you cannot find the first child of a not existing object.
How to fix it: I recommend using WaitForChild just in case they haven't loaded in yet.
ChatHandler:
local chatstorage = game.Workspace.ChatStorage local onChatInputted = game.ReplicatedStorage:WaitForChild("OnChatInputted") onChatInputted.OnServerEvent:Connect( function(plr, msg) local filteredText local filteredString local char = plr.Character or plr.CharacterAdded:Wait() local Head = char:WaitForChild("Head") local stringval = Instance.new("StringValue", chatstorage) local success, errormsg = pcall( function() filteredText = game:GetService("TextService"):FilterStringAsync(msg, plr.UserId) end ) if success then local success2, errormsg2 = pcall( function() filteredString = filteredText:GetNonChatStringForBroadcastAsync() end ) if success2 then local nameGui = Head:WaitForChild("NameGui") -- changed to waitforchild local nameLabel = nameGui:WaitForChild("NameLabel") -- changed to waitforchild stringval.Name = "[" .. plr.Team.Name .. "] " .. nameLabel.Text .. ": " .. string.upper(string.sub(filteredString, 1, 1)) .. string.sub(filteredString, 2) local RS = game:GetService("ReplicatedStorage") local Chat = RS:WaitForChild("Chat"):Clone() local Bubble = Chat:WaitForChild("Bubble") local TextLabel = Bubble:WaitForChild("TextLabel") TextLabel.Text = string.upper(string.sub(filteredString, 1, 1)) .. string.sub(filteredString, 2) Chat.Adornee = Head Chat.Parent = Head 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 else warn(errormsg2) stringval:Destroy() end else warn(errormsg) stringval:Destroy() end end )
ChangeNameScript:
local remoteEvent = game.ReplicatedStorage.NameChangeEvent remoteEvent.OnServerEvent:Connect( function(plr, name) local char = plr.Character or plr.CharacterAdded:Wait() local filteredName = game:GetService("TextService"):FilterStringAsync(name, plr.UserId) local filteredNameAsString = filteredName:GetNonChatStringForBroadcastAsync() local nameGui = char.Head:WaitForChild("NameGui") -- changed to waitforchild local nameLabel = nameGui:WaitForChild("NameLabel") -- changed to waitforchild nameLabel.Text = filteredNameAsString end )
Accept answer if it worked.