Hello, I am trying to make it so that when I click a hat model, the hat appears on the players head, I have started on the code but its error messaging me.
The code:
local ServerStorage = game:GetService(“ServerStorage”)
local RealHats = ServerStorage.HatStore:GetChildren()
local FakeHats = workspace:WaitForChild("FakeHats"):GetChildren()
local function GiveHat()
local Character = player.Character or player.CharacterAdded:Wait()
local HatCheck = Character:FindFirstChild(FakeHat.Name)
local Humanoid = Character:WaitForChild("Humanoid")
if not HatCheck then
for i, RealHat in pairs(RealHats) do
if FakeHat.Name == RealHat.Name then
local ClonedHat = RealHat:Clone()
Humanoid:AddAccessory(ClonedHat)
end
end
end
end
for i,FakeHat in pairs(FakeHats) do
local Handle = FakeHat:FindFirstChild("Handle")
local ClickDetector = Handle.ClickDetector
if Handle and ClickDetector then
ClickDetector.MouseClick:Connect(function(player)
GiveHat(player,FakeHat)
end)
end
end
This is the code on my script page where it shows the errors

If you have any further questions, do not hesitate to ask!