How to destroy tool in Player's Inventory/Backpack?

  1. What do you want to achieve?
    I’d like to use a proximity prompt to remove/destroy a tool from the player whether or not it’s in the player’s hand or backpack. I do not want the original tool to be destroyed, just the cloned tool that’s in the player’s inventory.

  2. What is the issue?
    I’ve written this script (a ServerScript within a ProximityPrompt); however, it’s not working the way I’d like it to. It gives the player money but doesn’t remove the tool from the player.

local pizza = game.Workspace.PupperoniPizza
local prp = script.Parent
local customer = prp.Parent

script.Parent.Triggered:Connect(function(player)
		if game.StarterPlayer["Pup Pizza"].Value == true then
		player.leaderstats.Money.Value = player.leaderstats.Money.Value + 25
		game.StarterPlayer["Pup Pizza"].Value = false
		player.Backpack:FindFirstChild("Pupperoni Pizza"):Destroy()
		player.Character:FindFirstChild("Pupperoni Pizza"):Destroy()
	
	end
	end)
  1. What solutions have you tried so far?
    I’ve read some forum posts about removing ALL the tools from the player but that isn’t specific to what I’m trying to achieve. It’s helped me write what I’ve written so far but my script isn’t working.

Screen Shot 2022-07-21 at 5.31.49 PM

It could be that you are trying to destroy something that doesn’t exist within somewhere


script.Parent.Triggered:Connect(function(player)
	
	
	local BackpackTool = player.Backpack:FindFirstChild("Pupperoni Pizza")
	local EquippedTool =  player.Character:FindFirstChild("Pupperoni Pizza")
	
	if BackpackTool  then
		BackpackTool:Destroy()
		print("BackPackTool Destroy")
	elseif EquippedTool then
		EquippedTool:Destroy()
		print("Equipped Destroy")
	end
		
end)
2 Likes

Have you got any errors in output?

I figured out how to do it for proximity prompts! But now I’m struggling with destroying the tool when it touches a part:

script.Parent.Touched:Connect(function(player, Hit)
	if Hit.Parent.Name == 'Brush' then
	local backpack = player.Backpack

	local char = player.Character
	if not char then return end

	local hum = char.Humanoid
	hum:UnequipTools()
	wait()

		backpack:FindFirstChild("Brush"):Destroy()
		end
end)

How would I destroy the tool from the player’s backpack when the tool touches a part?

remove the player param from the touched function, replace it with the “hit” instead

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

this should give you an idea on what to do next

1 Like

I’m getting this error in my output:

Workspace.NPCs.NPC.UpperTorso.RemoveToolScript:4: attempt to index nil with ‘Backpack’

script.Parent.Touched:Connect(function(Hit)
	local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
	if Hit.Name == 'Brush' then
	local backpack = player.Backpack

	local char = player.Character
	if not char then return end

	local hum = char.Humanoid
	hum:UnequipTools()
	wait()

		backpack:FindFirstChild("Brush"):Destroy()
		end
end)

very well, edit that one similar phrase

Players:GetPlayerFromCharacter(Hit.Parent.Parent)

2 Likes

It works!
THANK YOU :slight_smile:

script.Parent.Touched:Connect(function(Hit)
	local player = game.Players:GetPlayerFromCharacter(Hit.Parent.Parent)
	if Hit.Name == 'Brush' then
	local backpack = player.Backpack

	local char = player.Character
	if not char then return end

	local hum = char.Humanoid
	hum:UnequipTools()
	wait()

		backpack:FindFirstChild("Brush"):Destroy()
		end
end)
2 Likes

just make sure to make an if checker just in case it breaks since its trying to look for a nil object like checking for humanoids

May you please explain how to do that? I’m new to scripting so pardon my lack of knowledge.

I am getting an error from my other script:

Workspace.NPCs.NPC.UpperTorso.MoneyScript:8: attempt to index nil with ‘leaderstats’

My other script:

--local tool = game.ServerStorage["Brush"]
local giver = game.Workspace.BrushP
local prp = script.Parent
local customer = prp.Parent

script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent.Name == 'Brush' and game.StarterPlayer["Dye"].Value == true then
		game.Players:GetPlayerFromCharacter(Hit.Parent.Parent).leaderstats.Money.Value += 25
		game.StarterPlayer["Dye"].Value = false
		--	tool.Parent = game.ServerStorage

	end
end)

sorry but I’ll be heading out soon since its almost my sleeping schedule, I’ll come back later

1 Like

Okay! Rest well & I look forward to hearing from you! :slight_smile:

This error was occurring because it couldn’t find the “leaderstats” in Player class, since you were trying to parent out of it.

Here, I fixed it:

script.Parent.Touched:Connect(function(Hit)
	if Hit.Name == 'Brush' and game.StarterPlayer["Dye"].Value == true then -- a tool named brush
		game.Players:GetPlayerFromCharacter(Hit.Parent).leaderstats.Money.Value += 25 -- error was here
		game.StarterPlayer["Dye"].Value = false
	end
end)

I also re-did it a bit:

local STARTERPLAYER = game:GetService("StarterPlayer")
local PLAYERS = game:GetService("Players")

local PLR_NAME = "Brush"
local SHOULD_BE = true

local PART = script["Parent"]

PART["Touched"]:Connect(function(HIT)
	local MATCH = HIT["Parent"]["Name"]:match(PLR_NAME)
	local EQUAL = rawequal(SHOULD_BE, STARTERPLAYER["Dye"]["Value"])
	if MATCH and EQUAL then
		local PLAYER = PLAYERS:GetPlayerFromCharacter(HIT["Parent"])
		local MONEY = PLAYER["leaderstats"]["Money"]
		MONEY["Value"] += 25
		STARTERPLAYER["Dye"]["Value"] = false
	end
end)

Edit: Updated the original logic.

1 Like

“Brush” is supposed to be the tool’s name (not the player’s); however, I’ll try out what you’ve given me and see!

Oh, my bad, I didn’t know that. Usually, when a touch event is fired, the hit parameter is the “part/item” that touched it, so if you are trying to get a tool named “Brush”, you should not parent out of the Hit parameter for that because parenting out of that will return the player class instead.

Here, try this one instead:

local STARTERPLAYER = game:GetService("StarterPlayer")
local PLAYERS = game:GetService("Players")

local PART_NAME = "Brush"
local SHOULD_BE = true

local PART = script["Parent"]

PART["Touched"]:Connect(function(HIT)
	local MATCH = HIT["Name"]:match(PART_NAME)
	local EQUAL = rawequal(SHOULD_BE, STARTERPLAYER["Dye"]["Value"])
	if MATCH and EQUAL then
		local PLAYER = PLAYERS:GetPlayerFromCharacter(HIT["Parent"])
		local MONEY = PLAYER["leaderstats"]["Money"]
		MONEY["Value"] += 25
		STARTERPLAYER["Dye"]["Value"] = false
	end
end)
1 Like

Perhaps I’m mistaken… the first script you’ve provided me had the same error as before but the second one doesn’t increase the Money leaderstats Value but I don’t get an error :thinking:

Can you list where the “Dye” and “Part” are located in the Explorer? Perhaps a screenshot of them.

Edit: Assuming, it should be similar to this :

1 Like

Sure!

“Dye” is a BoolValue within StarterPlayer. The BoolValue is set to true once the clone of the tool is given to the player.

I’m assuming by “Part” you’re talking about the tool?
The original tool is in ServerStorage, then a clone of the tool is given to the player and is placed in the player’s inventory/backpack, then once the player touches the NPC (or the NPC’s UpperTorso), the player is then supposed to be given 25 Money & the clone of the tool is supposed to be removed from the player’s backpack/Inventory.

Screen Shot 2022-07-21 at 9.56.15 PM
Screen Shot 2022-07-21 at 9.56.01 PM

1 Like

Yes—It’s very similar to that; however, the NPC (with the touch part) is within a folder titled “NPCs” and then there are multiple NPCs titles “NPC”.

Turns out you were on the right track earlier.
This logic should work, it checks for the touch event, destroys the tool, and gives the player 25 Money.

Here, try this:

local STARTERPLAYER = game:GetService("StarterPlayer")
local PLAYERS = game:GetService("Players")

local PART_NAME = "Brush"
local SHOULD_BE = true

local PART = script["Parent"]

PART["Touched"]:Connect(function(HIT)
	local MATCH = HIT["Parent"]["Name"]:match(PART_NAME)
	local EQUAL = rawequal(SHOULD_BE, STARTERPLAYER["Dye"]["Value"])
	if MATCH and EQUAL then
		local PLAYER = PLAYERS:GetPlayerFromCharacter(HIT["Parent"]["Parent"])
		local CHARACTER = PLAYER["Character"]
		local LEADERSTATS = PLAYER["leaderstats"]
		local BACKPACK = PLAYER["Backpack"]
		CHARACTER[PART_NAME]:Destroy()
		LEADERSTATS["Money"]["Value"] += 25
		STARTERPLAYER["Dye"]["Value"] = false
	end
end)

Edit: Explorer with the “NPCs” folder:
image

1 Like