✓ Accepted answer
Usually, scripts for GUIs are LocalScripts. That's because GUIs are client-sided. So I'm assuming you're using a LocalScript; however, you're trying to access an object in ServerStorage which cannot be done because ServerStorage can only be accessed by the server. I would recommend that you use a RemoteEvent because if you clone something on the client, other players cannot see the item and it's just local to the player. Below is your script which I fixed up for you and left some comments for you to read.
local gamePassID = 9843545
script.Parent.ClickDetector.MouseClick:Connect(function(player)
if MarketplaceService:UserOwnsGamePassAsync(player.userId, gamePassID) then
game.ServerStorage["Hat"]:Clone().Parent = player.Accessory
if player:FindFirstChild("Hat") then
unequip then
wait (10)
end
end
end)