The Humanoid:GetAccessories() example has a code sample error

The following code sample on the Humanoid:GetAccessories() area has problematic code due to a logic error.

local Players = game:GetService("Players")

local function onPlayerAddedAsync(player)
	local connection = player.CharacterAppearanceLoaded:Connect(function(character)
			-- All accessories have loaded at this point
			local numAccessories = #character:GetAccessories()
			print(("Destroying %d accessories for %s"):format(numAccessories, player.Name))
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			humanoid:RemoveAccessories()
	end)

	-- Make sure we disconnect our connection to the player after they leave
	-- to allow the player to get garbage collected
	player.AncestryChanged:Wait()
	connection:Disconnect()
end

for _, player in Players:GetPlayers() do
	task.spawn(onPlayerAddedAsync, player)
end
Players.PlayerAdded:Connect(onPlayerAddedAsync)

Becomes

local Players = game:GetService("Players")

local function onPlayerAddedAsync(player)
	local connection = player.CharacterAppearanceLoaded:Connect(function(character)
			-- All accessories have loaded at this point
            local humanoid = character:FindFirstChildOfClass("Humanoid")
			local numAccessories = #humanoid:GetAccessories()
			print(("Destroying %d accessories for %s"):format(numAccessories, player.Name))
			humanoid:RemoveAccessories()
	end)

	-- Make sure we disconnect our connection to the player after they leave
	-- to allow the player to get garbage collected
	player.AncestryChanged:Wait()
	connection:Disconnect()
end

for _, player in Players:GetPlayers() do
	task.spawn(onPlayerAddedAsync, player)
end
Players.PlayerAdded:Connect(onPlayerAddedAsync)

The issue lies in using #character:GetAccessories as :GetAccessories is a function of Humanoid and not the players character.

Page URL: https://create.roblox.com/docs/reference/engine/classes/Humanoid#GetAccessories

1 Like

This is just an acknowledgment announcement!

We’ve filed a ticket into our internal database for this issue, and we will update you when we have further information!

Thanks for the report!

1 Like

Hi dylanjkl!

Thanks again for flagging this. I’ve confirmed the inaccurate code sample and replaced it with a working version based on your example. This may require a refresh to see on the documentation.

Have a great day!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.