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.
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)
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.
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)
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?
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)
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)
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)
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)
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)
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
“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.
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)