How do you keep a hat on a roblox character after they die?

My question is: How can I keep a characters hat on the character after it dies?

Usually when I make a ragdoll system of when the character dies, all of the hats fall off.
I Am trying to make all the hats stay on the character after it reaches 0 in health.

3 Likes

One of the option is to weld the accessories to the head of the character. Or alternatively, find out what is causing the accessory attachments to destroy and then prevent that from happening.

I Think the character’s hats just make themselves “uncollide” and then they just fall off the ground

And thing is that I Don’t know how can I get all of the hats of the character.

The accessories are part of the character model. All you need to do is to linear scan the model’s children and then weld them back. If that doesn’t work, something is causing the “attachments” to break as aforementioned.

Also there’s this property introduced not long ago:

Perhaps you can toggle that off?

3 Likes

You can add a script that re-welds the hats unto the player’s head when they die

You can use a for I, V in pairs loop to get all the hats.

I Still don’t know how to get all of the characters hats, I have never done it before.

you can go through all the descendants of the character to find accessories

local Des = Player.Character:GetDescendants() -- will give a array of everything


for i = 1,#Des do -- Loops for each item in a table / array
    if Des[i]:IsA("Accessory") then -- Checks the part type
        -- Create Weld to bodypart
    end
end
local char = script.Parent
local Des = char:GetDescendants() -- will give a array of everything
local weld = Instance.new("Weld")
local head = char.Head


for i = 1,#Des do -- Loops for each item in a table / array
	if Des[i]:IsA("Accessory") then -- Checks the part type
		-- Create Weld to bodypart
		weld.Part0 = head
		weld.Part1 = 
	end
end

I Wrote this script, How should the accessory part be called so I could weld it too?

The weld.Part0 and weld.Part1 connect to objects called ‘attachments’

The hat should already have attachments so all you will have to do is replace the current method of connections with a weld.

Also when you create a new weld you want that to be in the loop. There could be multiple hats so 1 weld wont do all of them.

1 Like

I Quite don’t get what you meant

Should I attach the welds to the attachments? or the hat itself?

the weld connects to two attachments to hold parts in place. The parts are what the attachment is on.

You mean the Handle of the accessory?

Welds need Part0 and Part1 to be baseparts, not attachments


The way I would go about doing this is saving the offsets of the accessories’ handles relative to their parent baseparts when they are all loaded, for which the CharacterAppearanceLoaded event will be used. Upon death, you would find the accessories and reweld them:

local savedAccessories = {}

Player.CharacterAppearanceLoaded:Connect(function()
    -- saving information about the accessories
    for _, part in ipairs(char:GetDescendants()) do
        if part:IsA("Accessory") then
            local cf = part.Parent.CFrame:inverse() * part.Handle.CFrame
            savedAccessories[#savedAccesories + 1] = {Handle = part.Handle; Parent = part.Parent; Offset = cf}
        end
    end
end)

Humanoid.Died:Connect(function()
    -- re-weld all accessories
    for _, accessoryData in ipairs(savedAccessories) do
        local weld = Instance.new("Weld")
        weld.Part0 = accessoryData.Parent
        weld.Part1 = accessoryData.Handle
        weld.C0 = accessoryData.Offset
        weld.Parent = accessoryData.Parent
    end
end)

Does game.Players work for this one?

I assume you mean for defining Player – you’re most likely using a server script to do this and I would advise that you parent the script to StarterCharacterScripts. You can get the player, character and humanoid using:

local player = game.Players:GetPlayerFromCharacter(script.Parent)
local char = script.Parent
local Humanoid = char:FindFirstChild("Humanoid")

The script from the start was in startercharacterscripts and thanks