Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Destroying a tool that is in hand inside a RemoteEvent?

Asked by
Zelt3q 31
4 years ago

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

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

0
I’m sure this will help, i’ll check it tomorrow when I get back on. Thank you so much :) Zelt3q 31 — 4y
0
Any updates on how it went? Don't forget to mark question as answered if this answers your question :P DevMaster333 215 — 4y
Ad
Log in to vote
0
Answered by
sebanuevo 123
4 years ago

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

Answer this question