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

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

Updated this Reply! Check the screenshot for the location of the Script.

1 Like

The script works! However, now it’s messing with the 3rd script within the NPC: :upside_down_face:

Output error:
Workspace.NPCs.NPC.UpperTorso.RemoveHairScript:2: attempt to index nil with ‘Name’

script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent.Name == 'Brush' then
		Hit.Parent.Color.Transparency = 1
		script.Sound:Play()
		wait(2)
		script.Parent.Parent:FindFirstChild("Purple").Handle.Transparency = 0
		script.Parent.Parent:FindFirstChild("White").Handle.Transparency = 1
		
		wait (35)
		script.Parent.Parent:FindFirstChild("Purple").Handle.Transparency = 1
		script.Parent.Parent:FindFirstChild("White").Handle.Transparency = 0
	end
end)

P.S. I truly appreciate you taking all this time to help me :slight_smile: It means a lot!

1 Like

Here, fixed:

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 EQUAL = rawequal(SHOULD_BE, STARTERPLAYER["Dye"]["Value"])
	local MATCH = HIT["Parent"]["Name"]:match(PART_NAME)
	local NPC = script["Parent"]["Parent"]
	local TOOL = HIT["Parent"]
	if MATCH and EQUAL then
		local PLAYER = PLAYERS:GetPlayerFromCharacter(HIT["Parent"]["Parent"])
		local CHARACTER = PLAYER["Character"]
		local LEADERSTATS = PLAYER["leaderstats"]
		TOOL["Color"]["Transparency"] = 1
		script["Sound"]:Play()
		CHARACTER[PART_NAME]:Destroy()
		LEADERSTATS["Money"]["Value"] += 25
		STARTERPLAYER["Dye"]["Value"] = false
		task["wait"](2)
		NPC:FindFirstChild("Purple")["Handle"]["Transparency"] = 0
		NPC:FindFirstChild("White")["Handle"]["Transparency"] = 1
		task["wait"](35)
		NPC:FindFirstChild("Purple")["Handle"]["Transparency"] = 1
		NPC:FindFirstChild("White")["Handle"]["Transparency"] = 0
	end
end)

Edit: Screenshot of the Explorer.

Edit 2: Re-wrote the logic, just so it’s a bit more readable.

local STARTERPLAYER = game:GetService("StarterPlayer")
local DYE = STARTERPLAYER:FindFirstChild("Dye")

local PLAYERS = game:GetService("Players")

local NPC = script["Parent"]["Parent"]
local OBJ:{table} = {
	[1] = NPC:FindFirstChild("Purple"),
	[2] = NPC:FindFirstChild("White")
}

local NAME = "Brush"
local BOOLEAN = true

function TOUCH(...):nil
	local PARAMETER:{table} = {...}
	local PLAYER = PARAMETER[1]["Parent"]["Parent"]
	local TOOL = PARAMETER[1]["Parent"]
	local EQUAL:boolean = rawequal(
		BOOLEAN,
		DYE["Value"]
	)
	local MATCH:boolean = string["match"](
		NAME,
		TOOL["Name"]
	)
	if MATCH and EQUAL then
		PLAYER = PLAYERS:GetPlayerFromCharacter(
			PLAYER
		)
		local CHARACTER = PLAYER["Character"]
		local LEADERSTATS = PLAYER["leaderstats"]
		TOOL["Color"]["Transparency"] = 1
		script["Sound"]:Play()
		CHARACTER[NAME]:Destroy()
		LEADERSTATS["Money"]["Value"] += 25
		DYE["Value"] = false
		task["wait"](2)
		OBJ[1]["Handle"]["Transparency"] = 0
		OBJ[2]["Handle"]["Transparency"] = 1
		task["wait"](35)
		OBJ[1]["Handle"]["Transparency"] = 1
		OBJ[2]["Handle"]["Transparency"] = 0
	end
end

local PART = script["Parent"]
PART["Touched"]:Connect(TOUCH)
1 Like

I think I may have corrected it! Let me know what you think—I was guessing this would work:

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
		script.Sound:Play()
		wait(2)
		PART["Parent"]:FindFirstChild("Purple").Handle.Transparency = 0
		PART["Parent"]:FindFirstChild("White").Handle.Transparency = 1
		wait (35)
		PART["Parent"]:FindFirstChild("Purple").Handle.Transparency = 1
		PART["Parent"]:FindFirstChild("White").Handle.Transparency = 0
	end
end)
1 Like

Yup, you actually did fix it, Nice!

Edit: But it seems like you forgot this part:

If you would like to include that part, you should do that a bit like this because the “Color” instance seems to be inside the tool itself and if you destroyed the tool before, you might have an error.

1 Like

Wowie! Thank you soooo much for your help! You’re absolutely amazing and I sincerely appreciate your help<3 You put in so much effort and were extremely patient with me :slight_smile: I hope to be as awesome as you in the future, I’m gonna follow you!

1 Like

Ah yes! I see— thanks for pointing that out! :slight_smile:

1 Like