Click Hat Equip

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
image

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

It seems that every time you put player it errors, you have no variable equal to player.
Edit: Also you forgot to put an s at the end of FakeHat in a bunch of areas.

1 Like

You have no variables for player or FakeHat.

1 Like

Realised the problem by myself, The givehat function didn’t have a value in the bracket