Trying to delete tool in players backpack

I’m trying to delete a tool in the players backpack after a click detector function has been detected. So I want a key to be deleted in the players backpack after they have clicked on a door to unlock it.

This is what I have so far:


local tool = game.ServerStorage["Key 1"]
local clone = tool:clone()
script.Parent.ClickDetector.MouseClick:connect (function(plr)
	if clone.Parent ~= plr.Backpack then 
		clone.Parent = plr.Backpack
	else
		
	end
end)

if workspace["Key 1"].ClickDetector.MouseClick:Connect (function(plr)
		

		
	end) then
	game.StarterPack:FindFirstChild("Key 1"):Destroy()
end

1 Like

Everything in StarterPack is actually cloned to the player’s backpack. Which means yes, these are two completely different things.

local tool = game.ServerStorage["Key 1"]
local clone = tool:clone()
script.Parent.ClickDetector.MouseClick:connect (function(plr)
	if clone.Parent ~= plr.Backpack then 
		clone.Parent = plr.Backpack
	else

	end
end)

workspace["Key 1"].ClickDetector.MouseClick:Connect(function(plr)
	local tool = plr.Backpack:FindFirstChild("Key 1")
	
	if tool then
		tool:Destroy()
	end
end)

It’s not working for me. The key is still there when the player does the click detector function.

If the player is using the tool then it’s not actually in the Backpack so you also need to check the Player.Character too:

local tool = game.ServerStorage["Key 1"]
local clone = tool:clone()
script.Parent.ClickDetector.MouseClick:connect (function(plr)
	if clone.Parent ~= plr.Backpack then 
		clone.Parent = plr.Backpack
	else

	end
end)

workspace["Key 1"].ClickDetector.MouseClick:Connect(function(plr)
	local tool = plr.Backpack:FindFirstChild("Key 1") or plr.Character:FindFirstChild("Key 1")
	
	if tool then
		tool:Destroy()
	end
end)

Can you show the error? Probably a mispell.

i did try the exact script above but the key still remains. Would I be able to do something within the door script rather than the key script?

The door script just checks if it’s the right key and opens up but doesn’t delete the key from the player.
Door Script:

active = true

function onTouched(hit)
	if active == true then
		if hit.Parent.Name == script.Parent.ToolRequired.Value then
			active = false 
			script.Parent.Unlock:Play()
			
			
			local TweenService = game:GetService("TweenService")

			local hinge = script.Parent.Parent.Hinge
			local prompt = script.Parent.ProximityPrompt

			local goalOpen = {}
			goalOpen.CFrame = hinge.CFrame * CFrame.Angles(math.rad(90), 0, 0)

			local goalClose = {}
			goalClose.CFrame = hinge.CFrame * CFrame.Angles(0, 0, 0)

			local tweenInfo = TweenInfo.new(1)
			local tweenOpen = TweenService:Create(hinge, tweenInfo, goalOpen)
			local tweenClose = TweenService:Create(hinge, tweenInfo, goalClose)
			
			prompt.Triggered:Connect(function()
				
				
				if prompt.ActionText == "Close" then
					tweenClose:Play()
					prompt.ActionText = "Open"
				else
					tweenOpen:Play()
					prompt.ActionText = "Close"
				end
			end)
			
			
		end
	end
end
script.Parent.Touched:connect(onTouched)

Looking at what has already been given to you, we can fix this. So assuming that there is a chance for the key to be either equipped/unequipped it might be a good idea to check both the character, and the backpack for the “Key 1” tool.

workspace["Key 1"].ClickDetector.MouseClick:Connect(function(plr)
	local tool = plr.Backpack:FindFirstChild('Key 1') or plr.Character:FindFirstChild('Key 1')
	if tool then
		tool:Destroy()
	end
end)
3 Likes

Heres a script that deletes the key if the key is held and clickdetector is clicked if thats what ur trinyg to do

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
    if plr.Character then
        local Key = plr.Character:FindFirstChild("Key 1")
        if Key then
            Key:Destroy()
        end
    end
end)

Okay I have managed to try a different part of code and this works for me. Thanks anyway for your suggestions.

workspace["Key 1"].ClickDetector.MouseClick:Connect(function(player)
	local ReplicatedKey = game.ReplicatedStorage["Key 1"]:Clone()
	local KeyOnFloor = workspace["Key 1"]
	ReplicatedKey.Parent = player.Backpack
	KeyOnFloor:Destroy()
end)


local door = script.Parent



local function remove(otherPart)

	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')

	local player = game.Players:FindFirstChild(otherPart.Parent.Name)

	if humanoid and player then

		local inHand = otherPart.Parent:FindFirstChildWhichIsA('Tool')

		local inBackpack = player.Backpack:FindFirstChildWhichIsA('Tool')

		if inHand then

			inHand:Destroy()

		end

		if inBackpack then

			inBackpack:Destroy()

		end

	end

end



door.Touched:Connect(remove)