This code sample automatically removes
Accessory objects like hats from the
Player's character when they respawn. Warning: this includes hair, so this script may cause acute baldness.
Player/CharacterAdded|added, we wait for
RunService/Stepped to fire once (using the
wait function of events). This is so the accessory removal logic runs one frame after the character spawns. A warning can appear if you delete accessories too quickly after the player spawns, so waiting one frame will avoid that.
local Players = game:GetService("Players") local RunService = game:GetService("RunService") -- Check if the given object is an Accessory (such as a hat) local function destroyAccessories(object) if object:IsA("Hat") or object:IsA("Accessory") then object:Destroy() end end local function onCharacterAdded(character) -- Wait a brief moment before removing accessories to avoid the -- "Something unexpectedly set ___ parent to NULL" warning RunService.Stepped:wait() -- Check for any existing accessories in the player's character for _, child in pairs(character:GetChildren()) do destroyAccessories(child) end -- Hats may be added to the character a moment after -- CharacterAdded fires, so we listen for those using ChildAdded character.ChildAdded:Connect(destroyAccessories) end local function onPlayerAdded(player) -- Listen for spawns player.CharacterAdded:Connect(onCharacterAdded) end Players.PlayerAdded:Connect(onPlayerAdded)