Removing the player accessories when the player touches the part

I need it for uniform system and it will remove the player accessories only once.

function onTouched(hit)
local d = hit.Parent:GetChildren()
	  for i=1, #d do
			if (d[i].className == "Accessory") then
				  d[i]:remove()
	  end
   end
end

script.Parent.Touched:connect(onTouched)
local function OnTouched(Hit)
   if not Hit.Parent:FindFirstChild("Humanoid") then return end
   for _,item in pairs(Hit.Parent:GetChildren()) do
        if item:IsA("Accessory") then
           item:Destroy()
           item = nil -- removing its reference
        end
   end
end

script.Parent.Touched:Connect(OnTouched)

You should define functions locally [ as long as they aren’t in a module], since local variables/functions are faster.

1 Like

Quite literally a function that does this that is just one line

Humanoid:RemoveAccessories()