How can i improve this assessory script

Hello devforum community,

i have a small problem i have a working accessorys script however i get a script warning wich annoys me and i want to know how i could improve this script so i dont get the annoying warning.

-- SERVERSCRIPT
remote.OnServerEvent:connect(function(player, request)
	local char = player.Character
	local hum = char:WaitForChild('Humanoid')
	if (request == 'HatOn') then
		if script.Parent.CoverHead.Value then
			for i, v in pairs(char:GetChildren()) do 
				if v:IsA('Accessory') or v:IsA('Hat') then
					v:FindFirstChild('Handle').Transparency = 1
				end
			end
		end
		script.Parent.Handle.Use:Play()
		hatmodel = accessories:FindFirstChild(script.Parent.HatName.Value):Clone()
		if hatmodel.Name == "TOWELONHEAD" or hatmodel.Name == 'BALACLAVA_CUSTOM' then
			local color = script.Parent.Color.Value.Selection.Value
			hatmodel.Handle:FindFirstChild('Mesh').VertexColor = Vector3.new(color.r, color.g, color.b)
		end
		hatmodel.Parent = char
		hum.DisplayDistanceType = 'None'
		script.Parent.Handle.Transparency = 1
	elseif (request == 'HatOff') then
		script.Parent.Handle.Use:Play()
		hatmodel:Destroy()
		if script.Parent.CoverHead.Value then
			for i, v in pairs(char:GetChildren()) do 
				if v:IsA('Accessory') or v:IsA('Hat') then
					v:FindFirstChild('Handle').Transparency = 0
				end
			end
		end
		script.Parent.Handle.Transparency = 0
		hum.DisplayDistanceType = 'Viewer'
	end	
end)

so my warning is that “hatmodel = accessories:FindFirstChild(script.Parent.HatName.Value):Clone()” should be consirered to changed local however when i change it to local in "elseif the code “hatmodel:Destroy()” becomes a unknown global wich is really annoying. so if i fix the warning i get another warning basicly.

wow thats really it ? but leavin it blank dosent it like do nothing or what ? i dont understand.

I see a few issues with this code, First the code is being treated as a Client Script, Seen as where you define HatModel in the “HatOn” and then destroy it inside of “HatOff”, You should consider asking if the Hat even exists in the first place, Which is as simple as just using FindFirstChild on the players character. I also notice you are probably running several of these scripts for several accessories which isn’t the most suitable. I recommend having the player request a hat to equip and then using a Lookup table or something similar.

im having a client script fire these events thats a serverscript doing putting the hat on the player.

but you are right i should first make sure it finds the player