Welds in tools getting deleted after cloned

Hi! So I found this script on the DevForum that saves tools on death:

local Players = game:GetService("Players")

function OnPlayerAdded(Player)

	local SavedTools = {}

	local function OnCharacterAdded(Character)

		for Index, Tool in pairs(SavedTools) do --Loads all the saved tools from the player's last death
			Tool.Parent = Player.Backpack
		end

		SavedTools = {}

		local function OnDied() --Copies all your tools when you die and saves them to a table attached to the player
			for Index, Tool in pairs(Player.Backpack:GetChildren()) do
				local CopiedTool = Tool:Clone()
				table.insert(SavedTools, CopiedTool)
			end

			for Index, Tool in pairs(Player.Character:GetChildren()) do --Checks if theres a tool being currently used/equipped by the player
				if Tool:IsA("Tool") then
					local CopiedTool = Tool:Clone()
					table.insert(SavedTools, CopiedTool)
				end
			end
		end

		Character.Humanoid.Died:Connect(OnDied)

	end

	Player.CharacterAdded:Connect(OnCharacterAdded)

end

for Index, Player in pairs(Players:GetChildren()) do --Redundency incase a player has joined before the event sets up
	OnPlayerAdded(Player)
end

Players.PlayerAdded:Connect(OnPlayerAdded)

However, when you die while holding the tool, it appears to be invisible after you respawn, when in reality, the welds that are holding the tool together get deleted. So the tool exist, just only the Handle is actually being held. Any help would be very much appreciated! :slight_smile:

2 Likes

Is Archivable set to true on the welds?

You could also do something like:

for i, tool in pairs(Player.Backpack:GetChildren()) do
				local CopiedTool = tool:Clone()
				table.insert(SavedTools, CopiedTool)
				if tool:FindFirstChildWhichIsA("Weld") then
					local Weld = tool.Weld
					Weld:Clone()
				end

Hope this helps! :slight_smile:

2 Likes

Yes, archivable is set to true.

After using your code to make the script look like this:

local Players = game:GetService("Players")

function OnPlayerAdded(Player)

	local SavedTools = {}

	local function OnCharacterAdded(Character)

		for Index, Tool in pairs(SavedTools) do --Loads all the saved tools from the player's last death
			Tool.Parent = Player.Backpack
		end

		SavedTools = {}

		local function OnDied() --Copies all your tools when you die and saves them to a table attached to the player
			for i, tool in pairs(Player.Backpack:GetChildren()) do
				local CopiedTool = tool:Clone()
				table.insert(SavedTools, CopiedTool)
				if tool:FindFirstChildWhichIsA("Weld") then
					local Weld = tool.Weld
					Weld:Clone()
				end
			end

			for Index, Tool in pairs(Player.Character:GetChildren()) do --Checks if theres a tool being currently used/equipped by the player
				if Tool:IsA("Tool") then
					local CopiedTool = Tool:Clone()
					table.insert(SavedTools, CopiedTool)
				end
			end
			
			
		end

		Character.Humanoid.Died:Connect(OnDied)

	end

	Player.CharacterAdded:Connect(OnCharacterAdded)

end

for Index, Player in pairs(Players:GetChildren()) do --Redundency incase a player has joined before the event sets up
	OnPlayerAdded(Player)
end

Players.PlayerAdded:Connect(OnPlayerAdded)

It still doesn’t work :confused:

2 Likes

My script looks like this:

local function onPlayerAdded(Player)
	local SavedTools = {}
	
	Player.CharacterAdded:Connect(function(Character)
		for i, tool in pairs(SavedTools) do
			tool.Parent = Player.Backpack
		end
		SavedTools = {}
		
		Character.Humanoid.Died:Connect(function()
			for i, tool in pairs(Player.Backpack:GetChildren()) do
				local CopiedTool = tool:Clone()
				table.insert(SavedTools, CopiedTool)
				if tool:FindFirstChild("Weld") then
					local Weld = tool:FindFirstChild("Weld")
					Weld.Archivable = true
				end
			end
		end)
	end)
end

for _, Player in pairs(game.Players:GetPlayers()) do
	onPlayerAdded(Player)
end

What type of welds are you using?

1 Like

I’m just using weld constraints. I’m pretty sure that the welds are the issue since everything else remains except for the welds.

2 Likes

I’m not sure what else the problem would be, but try this:

Character.Humanoid.Died:Connect(function()
			for i, tool in pairs(Player.Backpack:GetChildren()) do
				local CopiedTool = tool:Clone()
				table.insert(SavedTools, CopiedTool)
				if CopiedTool:FindFirstChildOfClass("WeldConstraint") then
					local Weld = CopiedTool:FindFirstChild("WeldConstraint")
					Weld:Clone()
				end
			end
		end)
2 Likes

What would the full script look like? I just want to make sure I’m putting it in the right place.

1 Like

How do you kill your players? Do you run :BreakJoints() on your player to kill them?

3 Likes

The players just die when their health reaches 0

2 Likes

This is the full script:

local function onPlayerAdded(Player)
	local SavedTools = {}
	
	Player.CharacterAdded:Connect(function(Character)
		for i, tool in pairs(SavedTools) do
			tool.Parent = Player.Backpack
		end
		SavedTools = {}
		
		Character.Humanoid.Died:Connect(function()
			for i, tool in pairs(Player.Backpack:GetChildren()) do
				local CopiedTool = tool:Clone()
				table.insert(SavedTools, CopiedTool)
				if CopiedTool:FindFirstChildOfClass("WeldConstraint") then
					local Weld = CopiedTool:FindFirstChild("WeldConstraint")
					Weld:Clone()
				end
			end
		end)
	end)
end

for _, Player in pairs(game.Players:GetPlayers()) do
	onPlayerAdded(Player)
end

Mine is in ServerScriptService.

With this script the tools don’t even save at all. Also, the welds only get deleted when you die while holding the tool. Not sure if I made that clear.

2 Likes

It works fine for me. Not sure what the problem is. Sorry :frowning:

2 Likes

Hi again. I have managed to fix this. All I did was instead of copying the tool from the player’s backpack, it copies it from serverstorage. Thanks for all of your help! :slight_smile:

2 Likes