How do I destroy a tool that is being held in the hand? I have currently done as if the tool is in the backpack it will be destroyed.
ReplicatedStorage.ShopFunctions:WaitForChild("Ninjago").OnServerInvoke = function(player) if player.leaderstats.Money.Value >= 500 and MyDebounce == true and player then MyDebounce = false player.leaderstats.Money.Value = player.leaderstats.Money.Value - 500 player.Backpack:ClearAllChildren() ServerStorage.Tools.AzureSword:Clone().Parent = player.Backpack end end
If the tool is located in his backpack, this is how you would do it:
What you want to do is first create a function to find the tool. Next, create a function to destroy the tool. Here's how:
local tool = ServerStorage.Tools.AzureSword local function FindItemFromBackpack(whichItemName, whichPlayer) local playerBackpack = whichPlayer.Backpack local playerBackpackChilds = playerBackpack:GetChildren() for _, eachItem in pairs(playerBackpackChilds) do if eachItem.Name == whichItemName then return eachItem end end end local function DestroyTool(whichTool, whichPlayer) -- Look through his backpack local toolInBackpack= FindItemFromBackpack(tool.Name, player) if toolInBackpack then toolInBackpack:Destroy() toolInBackpack = nil end end ReplicatedStorage.ShopFunctions:WaitForChild("Ninjago").OnServerInvoke = function(player) local myDebounce = true if player.leaderstats.Money.Value >= 500 and MyDebounce == true and player then MyDebounce = false player.leaderstats.Money.Value = player.leaderstats.Money.Value - 500 DestroyTool(tool, player) end end
If it's located in his character then you would look through his character and not his backpack. If you want to make sure it's neither in his backpack nor his character then you would look at both of them and destroy the both. Here's how:
local tool = ServerStorage.Tools.AzureSword local function FindItemFromBackpack(whichItemName, whichPlayer) local playerBackpack = whichPlayer.Backpack local playerBackpackChilds = playerBackpack:GetChildren() for _, eachItem in pairs(playerBackpackChilds) do if eachItem.Name == whichItemName then return eachItem end end end local function FindItemFromCharacter(whichItemName, whichPlayer) local charItems = whichPlayer.Character:GetChildren() for _, eachItem in pairs(charItems) do if eachItem.Name == whichItemName then return eachItem end end end local function DestroyTool(whichTool, whichPlayer) -- Look through his backpack local toolInBackpack= FindItemFromBackpack(tool.Name, player) if toolInBackpack then toolInBackpack:Destroy() toolInBackpack = nil end -- Look through his character local toolInChar = FindItemFromCharacter(tool.Name, player) if toolInChar then toolInChar:Destroy() toolInChar = nil end end ReplicatedStorage.ShopFunctions:WaitForChild("Ninjago").OnServerInvoke = function(player) local myDebounce = true if player.leaderstats.Money.Value >= 500 and MyDebounce == true and player then MyDebounce = false player.leaderstats.Money.Value = player.leaderstats.Money.Value - 500 DestroyTool(tool, player) end end
Let me know how it goes.
Tools that are being hold are in the player's character, so u can do
if player.Character:FindFirstChildOfClass("Tool") then player.Character:FindFirstChildOfClass("Tool"):Destroy() end