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

The script shows this: CFrame is not a valid member of Model “Workspace.nomcell”

What part is the CFrame for? .

I think what you want to do is use WeldConstraints. That may stop them from falling of.

Under the assumption that the accessories are parented to their connecting baseparts, I thought it would work, but that’s not the case :stuck_out_tongue:

This is what I have now:

local savedAccessories = {}

Player.CharacterAppearanceLoaded:Connect(function()
    -- saving information about the accessories
    for _, part in ipairs(char:GetDescendants()) do
        if part:IsA("Accessory") then
            local handle = part.Handle
            local weld = part:FindFirstChildOfClass("Weld")
            local parent = weld.Part1
            local cf = parent.CFrame:inverse() * handle.CFrame
            savedAccessories[#savedAccesories + 1] = {Handle = handle; Parent = 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)

The 12th line gives errors with the weld

What is the 12th line exactly and the error? I wouldn’t know how your script looks exactly so it helps if you actually post the line and the error thrown

I Tried mixing up both scripts and I Made this:
(forget line 12)

local char = script.Parent
local Des = char:GetDescendants() -- will give a array of everything
local weld = Instance.new("Weld")
local head = char.Head
local h = char.Humanoid
local savedAccessories = {}
local Player = game.Players:GetPlayerFromCharacter(script.Parent)
local weld = Instance.new("Weld")

weld.Part1 = 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
		
	end
end

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

h.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)

local weld = part:FindFirstChildOfClass(“Weld”)
This is the part of the error

Workspace.nomcell.KeepHatsOnDeath:25: attempt to index nil with ‘Part1’
and this is the error

replace that with:

local weld = handle:FindFirstChildOfClass("Weld")
1 Like

It changed nothing…
hats still fall of the map

I Have tried changing some stuff and nothing really worked out…

I Still cannot find any solution for the error, thanks everyone for trying to help me.