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

How To Remove All Tools From A Person's Backpack When They Touch A Part?

Asked by 3 years ago
Edited 3 years ago

I wanted To Make a Trap Card Sorta Thing On My Red vs Blue Game Im Working On, I Already Got The Buying Trap And Placing It Down Working, But I Can't Figure Out How To Make The Remove All Tools From The Player's Backpack, So How Do I Do It? (also, it also needs to remove the player's hand tool)

2 answers

Log in to vote
2
Answered by
ElBamino 153
3 years ago

There's a few different ways of doing this. Below I will provide you with a few different options depending on your wants and needs. If you're looking to remove all the tools from a players backpack (this would not include anything equipped I'm doing this first because, you said remove all tools from the players backpack) you would use this sample. (This works)

script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        for t, item in pairs (game.Players[hit.Parent.Name].Backpack:GetChildren()) do--this removes anything in backpack
            if item:IsA("Tool") then
                item:Destroy()
            end
        end
    end
end)

If you're looking to remove everything including equipped tools you would use this.

script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        for t, item in pairs (hit.Parent:GetChildren()) do--this removes anything equipped
            if item:IsA("Tool") then
                item:Destroy()
            end
        end
        for t, item in pairs (game.Players[hit.Parent.Name].Backpack:GetChildren()) do--once again this for the backpack
            if item:IsA("Tool") then
                item:Destroy()
            end
        end
    end
end)

There's more than one way to do this so here's a code that does the same thing. Like I said you can make your selection. Depending on your wants and needs, the other ones are really good for using certain things like, lets say removing a certain "Sword" or something you can simply add a item.Name == "whatever your tool name is like I said sword" The code below once again is only for removing tools in the backpack like you asked for!

function hit(part)
    if hit == nil then return end
    local h = game.Players:GetPlayerFromCharacter(part.Parent)
    if h ~= nil then
        local tool = h.Backpack:GetChildren()
        for i=1,#tool do
            tool[i]:Destroy()
        end
    end
end

script.Parent.Touched:connect(hit)

I actually tested these, if you have any problems feel free to comment and let me know! I'm happy to help.

0
This is a "normal script" you would put inside of a part by the way! ElBamino 153 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
local Part = script.Parent

Part.Touched:Connect(function(Touch)

    if Touch and Touch.Parent then

        if Touch.Parent:FindFirstChild("Humanoid") then

            local Player = game:GetService("Players"):GetPlayerFromCharacter(Touch.Parent)

            if Player then

                local Backpack = Player:WaitForChild("Backpack")

                local Character = Touch.Parent or Player.Character

                for _,Child in pairs(Backpack:GetChildren() and Character:GetChildren()) do

                    if Child:IsA("Tool") then

                        Child:Destroy()
                    end
                end
            end
        end
    end
end)

Answer this question