PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

RemoveAccessories

This function removes all Accessory|Accessories worn by the Humanoid.

How does RemoveAccessories work?

When this function is called, all Accessory|Accessories sharing an Instance/Parent with the Humanoid will be removed. For Player, Player/Character|Characters this will remove all hats being worn as hats take the form of Accessory|Accessories in the data model.

Instance/Destroy is called on the removed Accessory|Accessories, meaning their Instance/Parent|Parents are set to nil and locked.

The Humanoid does not need to belong to a Player Player/Character for this to work. This function will remove Accessory|Accessories parented to the Humanoid|Humanoid's Instance/Parent regardless of where the Humanoid is.

See also

  • To attach an Accessory use the Humanoid/AddAccessory function
  • To get all Accessory|Accessories belonging to a Humanoid use the Humanoid/GetAccessories function

Returns

Return Type Summary

Code Samples


Remove and Return Hats

This code sample will remove all the hats from a Player/Character once the character has fully loaded. After five seconds, if the character still exists and is alive, it will return them.

local Players = game:GetService("Players")

local function playerAdded(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		-- save hats for later
		local accessories = {}
		for _, accessory in pairs(humanoid:GetAccessories()) do 
			table.insert(accessories, accessory:Clone())
		end

		-- remove hats
		humanoid:RemoveAccessories()

		wait(5)

		-- make sure the player still exists, and has the same character
		if player and player.Character and player.Character == character then
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			if humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
				-- give back the hats
				for _, accessory in pairs(accessories) do 
					humanoid:AddAccessory(accessory)
				end
			end
		end

		accessories = nil
	end)
end

-- get existing players
for _, player in pairs(Players:GetPlayers()) do 
	playerAdded(player)
end
-- listen for new players
Players.PlayerAdded:Connect(playerAdded)