CreateChat "!hat" "" (5) [[HatMenu:OpenPlayer(id,1)]]

Hats = {}
function AddHat(name, dir)
	Hats[name] = dir
end

for i = 1, #Hats do
	AddTransferFile(Hats[i])
end

HatMenu = CreateMenu("Hats")
function HatMenu:getcustombutton(b)
	local i = 0
	for k, v in pairs(Hats) do
		i = i + 1
		if i == b then return v end
	end
end
AddMenu(HatMenu, 0)

function HatMenu:click(id,b,p)
	if b > 1 then
		if USERTEMP[id]["hat"] then
			freeimage(USERTEMP[id]["hat"])
		end
	
		local i = 0
		for k, v in pairs(Hats) do
			i = i + 1
			if i == b then
				USERTEMP[id]["hat"] = image(v, 1, 0, 200+id)
				break
			end
		end
	end
end

addhook("leave","player_hat_remove")
function player_hat_remove(id)
	if USERTEMP[id]["hat"] then
		freeimage(USERTEMP[id]["hat"])
	end
end

addhook("startround_prespawn","hat_remove")
function hat_remove()
	for i = 1, 32 do
		USERTEMP[i]["hat"] = nil
	end
end

AddHat("Devil", "gfx/hat/devil.bmp")
AddHat("Headcrab", "gfx/hat/headcrab.bmp")
AddHat("Helmet", "gfx/hat/helmet.bmp")
AddHat("Police", "gfx/hat/police.bmp")