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.
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.
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:
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?
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)
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")