Heyo,
I’ve been developing a game recently and one feature I’d like to implement are custom nametags for all the NPCs in the World.
From this:
To this:
Instead of manually adding this in, I want to instead make a script that would automatically add those nametags as well as hide the existing ones using arrays (in case I add lots of NPCs)
Pseudocode:
1. Get children from workspace
2. If child is a Model, get the model's children
3. Look for Humanoid and Head (to determine it is an NPC)
4a. If nametag exists, return end
4b. If nametag isn't created, create the nametag under Head
5. Hide default nametag
Code:
--Makes custom nametags for all Humanoids
local Wk = game:GetService("Workspace")
local Hm = Wk:GetChildren()
while true do
wait(0)
for m = 1, #Hm do
if Hm[m].ClassName == "Model" then
local AF = Hm[m]:GetChildren()
local Headtag = AF:FindFirstChild("Head").Nametag --The Problematic line
if Headtag then
return
else
if AF.Humanoid ~= nil then
if AF.Head ~= nil then
AF.Humanoid.DisplayDistanceType = "None"
local Tag = Instance.new("BillboardGui",Hm[m]:findFirstChild("Head"))
Tag.Name = "Nametag"
Tag.ExtentsOffset = Vector3.new(0,2,0)
Tag.MaxDistance = 25
Tag.LightInfluence = 0
local Nme = Instance.new("TextLabel", Tag)
Nme.Name = "Namebox"
Nme.BackgroundTransparency = 1
Nme.TextSize = 25
Nme.TextColor3 = Color3.new(255,255,255)
Nme.Font = "Cartoon"
Nme.Text = (Hm.Name)
else
print(script.Name.." > No Head found.")
end
else
print(script.Name.." > No Humanoid found.")
end
end
end
end
end
All I get is an “attempt to call method ‘FindFirstChild’ a nil value”
Same thing happens when I use WaitForChild as well.
I’m not the best at scripting, nor using arrays.
Aand it’s my first time posting something to the forums