Sword Weld Doesn't Save After Player Dies

Hello devforum! I tried welding a sword MODEL to a part of the player’s leg, which when equipped, the model destroys, and when unequipped, the model reappears on the leg. It works perfectly fine, but after the player dies, the sword doesn’t appear on the leg anymore, due to it being parented to nil apparently?

Here’s my script:

local Handle = script.Parent.Handle

game.ReplicatedStorage.KatanaWeld.OnServerEvent:Connect(function(player)
	while wait() do
		if script.Parent.Parent.Name == "Backpack" then
			local Chararacter = script.Parent.Parent.Parent.Character
			
			if Chararacter then
				local UpperTorso = Chararacter:FindFirstChild("UpperTorso")
				local Weapon = Chararacter:FindFirstChild(script.Parent.Name)

				if UpperTorso and not Weapon then
					local WeaponOnLeg = Instance.new("Model")
					WeaponOnLeg.Name = script.Parent.Name
					WeaponOnLeg.Parent = Chararacter
					
					Handle = script.Parent.Handle:Clone()
					Handle.Name = "Handle"
					Handle.Parent = WeaponOnLeg
					
					Handle.Handle.Material = "SmoothPlastic"
					Handle.Handle.Reflectance = 0

					local LegWeld = Instance.new("Weld")
					LegWeld.Name = "WeldOnLeg"
					LegWeld.Part0 = UpperTorso
					LegWeld.Part1 = Handle
					LegWeld.C0 = CFrame.new(1,-1.25,0)
					LegWeld.C0 = LegWeld.C0 * CFrame.fromEulerAnglesXYZ(80, -20.5,80)
					LegWeld.Parent = Handle
				end
			end

		else
			if Handle.Parent then 
				Handle.Parent:Destroy()
			end
		end
	end
end)

The compiler returns nil as a parent because it is still tracking the old character, which was deleted when you died. We must instead tell the compiler where the new character’s model is located.

I don’t know how your remotes are set up, but I assume this will help:

game.Players.PlayerAdded:Connect(function(player)
    --Code here is executed when player joins
    player.CharacterAdded:Connect(function(character)
        --Code here is executed every time character spawns

        --Place the KatanaWeld.OnServerEvent function in here.
        --Every time the character respawns, the function will be reconnected and assigned to the new character.
    end
end
1 Like

You could instead use Player.CharacterAdded to refer to the newest (most recent) character model pertaining to a single player instance.

game.ReplicatedStorage.KatanaWeld.OnServerEvent:Connect(function(player)
	local Character = player.Character --this will refer to the latest character model of the player whenever this event is fired
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local HandleInHandle = Handle:WaitForChild("Handle")
local RS = game:GetService("ReplicatedStorage")
local Katana = RS:WaitForChild("KatanaWeld")
local isEquipped = false

Katana.OnServerEvent:Connect(function(Player) --how often is this event fired?
	local Character = Player.Character
	local UpperTorso = Character:FindFirstChild("UpperTorso")
	while task.wait() do --does this really need to execute every frame?
		if Character then
			if UpperTorso and not isEquipped then
				local WeaponOnLeg = Instance.new("Model")
				WeaponOnLeg.Name = Tool.Name
				WeaponOnLeg.Parent = Character

				local HandleClone = Handle:Clone()
				HandleClone.Name = "Handle"
				HandleClone.Parent = WeaponOnLeg
				HandleClone.Handle.Material = "SmoothPlastic"
				HandleClone.Handle.Reflectance = 0

				local LegWeld = Instance.new("Weld")
				LegWeld.Name = "WeldOnLeg"
				LegWeld.Part0 = UpperTorso
				LegWeld.Part1 = HandleClone
				LegWeld.C0 = CFrame.new(1,-1.25,0)
				LegWeld.C0 = LegWeld.C0 * CFrame.fromEulerAnglesXYZ(80, -20.5,80)
				LegWeld.Parent = HandleClone
			end
		end
	end
end)

Tool.Equipped:Connect(function()
	isEquipped = true
end)

Tool.Unequipped:Connect(function()
	isEquipped = false
end)

I added 2 comments regarding questions I had, I did add some fixes/improvements.

Thank you for the comments. I tried all ways but when the character spawns in, the sword is not welded to anything at all, it is parented to nil im pretty sure.