Drop tools on death

Hello so i have a Problem that when u die the player drops all there tools from the BackPack but they doesn’t disapper only aslong as i pick them up i want that like 15 Seconds later the tool disappers if nobody picked the tool up

local character = script.Parent
local humanoid = character:FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(character)
local backpack = player:WaitForChild("Backpack")

local hadToolEquipped = false

humanoid.Died:Connect(function()
    for i, tool in pairs(character:GetChildren()) do
        if tool:IsA("Tool") then
            hadToolEquipped = true
            tool.Parent = game.ReplicatedStorage
        end
    end
end)

player.CharacterRemoving:Connect(function(character)
    for i, tool in pairs(backpack:GetChildren()) do
        if tool:IsA("Tool") and tool:FindFirstChild("Handle") then
            tool.Parent = game.Workspace
            tool.Handle.CFrame = character.HumanoidRootPart.CFrame
        end
    end
    if hadToolEquipped ==true then
        local equippedTool = game.ReplicatedStorage:FindFirstChildOfClass("Tool")
        equippedTool.Parent = game.Workspace
        equippedTool.Handle.CFrame = character.HumanoidRootPart.CFrame
    end
end)

1 Like

You have the dropping code from what I see. What you can do is to wait for 15 seconds, check if the parent of the tool is workspace and if it is, destroy it, though you need to use task.spawn so the wait doesn’t halt the thread

Something like this

task.spawn(function()
    tool.Parent = game.Workspace
    tool.Handle.CFrame = character.HumanoidRootPart.CFrame
    task.wait(15)
    if tool.Parent ~= workspace then
        return
    end
    
    tool:Destroy()
end)
1 Like

where to put it into my script at what line should i insert the code?

The parts where you move the tools to workspace

This is my new script doesn`t work

local character = script.Parent
local humanoid = character:FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(character)
local backpack = player:WaitForChild("Backpack")

local hadToolEquipped = false

humanoid.Died:Connect(function()
	for i, tool in pairs(character:GetChildren()) do
		if tool:IsA("Tool") then
			hadToolEquipped = true
			tool.Parent = game.ReplicatedStorage
		end
	end
end)

player.CharacterRemoving:Connect(function(character)
	for i, tool in pairs(backpack:GetChildren()) do
		if tool:IsA("Tool") and tool:FindFirstChild("Handle") then
			tool.Parent = game.Workspace
			tool.Handle.CFrame = character.HumanoidRootPart.CFrame
			task.spawn(function()
				tool.Parent = game.Workspace
				tool.Handle.CFrame = character.HumanoidRootPart.CFrame
				task.wait(15)
				if tool.Parent ~= workspace then
					return
				end

				tool:Destroy()
			end)
		end
	end
	if hadToolEquipped ==true then
		local equippedTool = game.ReplicatedStorage:FindFirstChildOfClass("Tool")
		equippedTool.Parent = game.Workspace
		equippedTool.Handle.CFrame = character.HumanoidRootPart.CFrame
	end

Try changing this part to

for i, tool in ipairs(backpack:GetChildren()) do
    if tool:IsA("Tool") and tool:FindFirstChild("Handle") then
        task.spawn(function()
            tool.Parent = workspace
            tool.Handle.CFrame = character.HumanoidRootPart.CFrame
            task.wait(15)
            if tool.Parent == workspace then
                tool:Destroy()
            end
        end)
    end
end

Still doesn’t work that is my code:

local character = script.Parent
local humanoid = character:FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(character)
local backpack = player:WaitForChild("Backpack")

local hadToolEquipped = false

humanoid.Died:Connect(function()
	for i, tool in pairs(character:GetChildren()) do
		if tool:IsA("Tool") then
			hadToolEquipped = true
			tool.Parent = game.ReplicatedStorage
		end
	end
end)

player.CharacterRemoving:Connect(function(character)
	for i, tool in ipairs(backpack:GetChildren()) do
		if tool:IsA("Tool") and tool:FindFirstChild("Handle") then
			task.spawn(function()
				tool.Parent = workspace
				tool.Handle.CFrame = character.HumanoidRootPart.CFrame
				task.wait(15)
				if tool.Parent == workspace then
					tool:Destroy()
				end
			end)
		end
	end
	if hadToolEquipped ==true then
		local equippedTool = game.ReplicatedStorage:FindFirstChildOfClass("Tool")
		equippedTool.Parent = game.Workspace
		equippedTool.Handle.CFrame = character.HumanoidRootPart.CFrame
	end
end)

Okay I think I figured out something that could work

Put this code inside of a script in ServerScriptService

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	
	player.CharacterAdded:Connect(function(character)

		character.Humanoid.Died:Connect(function()
			character.Humanoid:UnequipTools()	
			local cframe = character.HumanoidRootPart.CFrame
			
			for i, tool in ipairs(player.Backpack:GetChildren()) do
				task.spawn(function()
					local handle = tool.Handle
					
					handle.Name = "TemporaryName"
					tool.Parent = workspace
					handle.CFrame = cframe

					player.CharacterAdded:Wait()
					handle.Name = "Handle"

					handle.Parent = nil
					handle.Parent = tool

					task.wait(15)
					if tool.Parent ~= workspace then
						return
					end
					tool:Destroy()
				end)
			end
		end)
	end)
end)

I think what was happening was that your dead body was picking up the tools again, I think the fix for that would be to rename the Handle until the player respawns, then wait the amount of time before destroying so that way they’d can’t pick up the tools again whilst they’re dead

1 Like

That wasn’t the error the error was that it still didn’t deleted the tool after 15 seconds

Also I Parent the tool into the ReplicatedStorage when the player is dead and when the character is not there anymore I do the tool into the workspace.