Player's equipped tool gets removed when dying in void

I have a script that saves all tools the player has (with the exception of certain tools). However, when falling into the void with an equipped tool, when respawning, the player will no longer have that equipped tool as it is destroyed.

I would assume there’s a simple property around this that I’m not seeing. I tried making a script that after falling to a certain depth, the player’s tool gets unequipped and cannot re-equip it, but that’s not optimal.

I want the player to respawn with all their tools, so I do not want the equipped tool to be destroyed in the void.

local Players = game:GetService("Players")

function OnPlayerAdded(Player)
	local SavedTools = {}

	local function OnCharacterAdded(Character)
		for Index, Tool in pairs(SavedTools) do
			Tool.Parent = Player.Backpack
		end

		SavedTools = {}

		local function OnDied()
			for Index, Tool in pairs(Player.Backpack:GetChildren()) do
				local CopiedTool = Tool:Clone()
				if not IsExcludedTool(CopiedTool) then
					table.insert(SavedTools, CopiedTool)
				end
			end

			for Index, Tool in pairs(Player.Character:GetChildren()) do
				if Tool:IsA("Tool") then
					local CopiedTool = Tool:Clone()
					if not IsExcludedTool(CopiedTool) then
						table.insert(SavedTools, CopiedTool)
					end
				end
			end
		end

		Character.Humanoid.Died:Connect(OnDied)
	end

	Player.CharacterAdded:Connect(OnCharacterAdded)
end

function IsExcludedTool(Tool)
	local ExcludedTools = {
		"ClassicSword",
		"UniSmash",
		"UnicornSword",
		"VipSword",
		"SpeedCoil",
		"RainbowMagicCarpet",
		"GravityCoil",
		"Boombox",
		"HandRed&HandBlue (PPTool)"
	}

	for _, ExcludedName in ipairs(ExcludedTools) do
		if Tool.Name == ExcludedName then
			return true
		end
	end

	return false
end

for _, Player in ipairs(Players:GetPlayers()) do
	OnPlayerAdded(Player)
end

Players.PlayerAdded:Connect(OnPlayerAdded)
3 Likes

You will need to store your tools outside the default Roblox tool handling in some sort of table, even if they equip the tool. The player’s Backpack is cleared every time the player respawns and the tool in the character is not sent back when dying or destroyed physically. Keeping track of all the tools outside the physical tools is your optimal solution.

Have a table store all the tools they player gets, including what was in their hand every time a tool is added. If you want to avoid the tool being destroyed and not returned to Backpack for your use, you need to save the equip tool externally.

4 Likes

I think you can use the StarterPack (StarterPack | Documentation - Hub Création Roblox)
Objects in StarterPack (such as tools) will always go back in the player’s backpack each time he spawns (and respawns).
You can modify the player’s StarterPack individually and at any time, adding or even removing tools from it.

1 Like

From what I read and know, it doesn’t seem that would work. The StarterPack is accessible to all players, so everyone would have the tool if it were to be placed in the StarterPack. I don’t see how I could change the StarterPack for one individual player.

Then save individually, for each player, in a table which tools should he have, and each time his character is added (when he respawns), add him back his tools in his backpack. I can provide code later if you want, I don’t have it in mind rn

I think this works:

local Players = game:GetService("Players")

local function OnPlayerAdded(Player)
	
	local SavedTools = {}

	local function OnCharacterAdded(Character)
		for _, Tool in pairs(SavedTools) do
			Tool.Parent = Player.Backpack
		end
		SavedTools = {}

		local function OnToolAdded(tool)
			if not IsExcludedTool(tool) then
				table.insert(SavedTools, tool:Clone())
			end
		end

		Player.Backpack.ChildAdded:Connect(OnToolAdded)
	end

	Player.CharacterAdded:Connect(OnCharacterAdded)
end

function IsExcludedTool(Tool)
	local ExcludedTools = {
		"ClassicSword",
		"UniSmash",
		"UnicornSword",
		"VipSword",
		"SpeedCoil",
		"RainbowMagicCarpet",
		"GravityCoil",
		"Boombox",
		"HandRed&HandBlue (PPTool)"
	}

	for _, ExcludedName in ipairs(ExcludedTools) do
		if Tool.Name == ExcludedName then
			return true
		end
	end

	return false
end

Players.PlayerAdded:Connect(OnPlayerAdded)
for _, Player in ipairs(Players:GetPlayers()) do
	OnPlayerAdded(Player)
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.