How could I improve this accessory's server 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.

Have you tried making a local variable under the char and hum variables (outside the scope of the if statements) and then assigning the accessory clone to that?

1 Like

wops sorry i forgot about this post.

problem was solved all i had to do is define it globaly