while true do
wait()
local player = game.Players.LocalPlayer
local plr = game.Players.LocalPlayer.Character
if player then
if plr then
if player.HatEquiped.Value ~= "Empty" then
for i,v in pairs(plr:GetChildren()) do
if v.ClassName == "Hat" then
local verification = v:FindFirstChild("HatVerifier")
if verification then
local hatwearing = v.Name
if player.HatEquiped.Value ~= hatwearing then
v:Destroy()
local hatnowwearing = game.ReplicatedStorage:FindFirstChild(""..player.HatEquiped.Value..""):Clone()
if hatnowwearing then
hatnowwearing.Parent = plr
end
end
elseif verification == nil then
local hatnowwearing = game.ReplicatedStorage:FindFirstChild(""..player.HatEquiped.Value..""):Clone()
if hatnowwearing then
hatnowwearing.Parent = plr
end
end
end
end
end
end
end
end
The specific issue is on line 21, it says that it's trying to detect a nil value
Basically, this is a script for character customization in my game. Each hat in my game has a value in it to distinguish it from Roblox Avatar hats. In line 21 it tries to make a value to Clone a hat from Replicated Storage if it's name is the same as the value of the hat that the character is supposed to be wearing. I tried to use FindFirstChild so that it wouldn't have the issue, but it is anyways. Help?

2 comments
The data type isn't important as it's just there (with no value in it) to show that the hat is part of my game.
Also, the hierarchy isn't important either, as this follows the hierarchy perfectly. It just has some issues with FindFirstChild giving errors.