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

Destroying previous tool on equip?

Asked by 4 years ago

This feels like it should be so easy but I can't figure it out. When the player picks up a new tool, I want their currently equipped tool to be destroyed. I tried this:

function Equipped(Mouse)
    Handle.Anchored = false
    Character = Tool.Parent
    Humanoid = Character:FindFirstChild("Humanoid")
    Torso = Character:FindFirstChild("HumanoidRootPart")
    Player = Players:GetPlayerFromCharacter(Character)

    Character:FindFirstChildOfClass("Tool"):Destroy()
    --This line is the important one^^^
end

but it tries to destroy the tool that is being equipped - the parent of the script. I could probably do a longer system with ChildAdded and classname == "Tool" but I feel like this should be an easy thing to do. Any thoughts?

1
When you find the first child of an object, it will show the longest existing one. Maybe try renaming the new tool for a second, making an if statement regarding the name, and then changing the name back? KDarren12 705 — 4y
0
Whoops, I forgot about this question. I decided to do it the basic way with a system that destroys the parent when a new tool is equipped. Thanks for the help everyone! tygerupercut3 68 — 4y

2 answers

Log in to vote
1
Answered by
Fad99 286 Moderation Voter
4 years ago
function Equipped(Mouse)
    Handle.Anchored = false
    local  Character = Tool.Parent
    local Children = Character:GetChildren()
    local Humanoid = Character:FindFirstChild("Humanoid")
    local Torso = Character:FindFirstChild("HumanoidRootPart")
    local Player = Players:GetPlayerFromCharacter(Character)

    for i,v in paris(Children) do
        If v.ClassName == "Tool" then
            If v.Name ~= "Tool.Name" then
                v:Destroy()
            end
        end
    end
end

What this does is Looks for all tools located in the character and deletes any that have a different name than the one equipped.

Ad
Log in to vote
1
Answered by 4 years ago

It's as simple as logging the last equipped tool, for example

local previoustool

tool.Equipped:connect(function() if previoustool then previoustool:Remove() end previoustool=tool end)

Answer this question