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)

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

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

01script.Parent.Touched:Connect(function(hit)
02    local humanoid = hit.Parent:FindFirstChild("Humanoid")
03    if humanoid then
04        for t, item in pairs (hit.Parent:GetChildren()) do--this removes anything equipped
05            if item:IsA("Tool") then
06                item:Destroy()
07            end
08        end
09        for t, item in pairs (game.Players[hit.Parent.Name].Backpack:GetChildren()) do--once again this for the backpack
10            if item:IsA("Tool") then
11                item:Destroy()
12            end
13        end
14    end
15end)

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!

01function hit(part)
02    if hit == nil then return end
03    local h = game.Players:GetPlayerFromCharacter(part.Parent)
04    if h ~= nil then
05        local tool = h.Backpack:GetChildren()
06        for i=1,#tool do
07            tool[i]:Destroy()
08        end
09    end
10end
11 
12script.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
01local Part = script.Parent
02 
03Part.Touched:Connect(function(Touch)
04 
05    if Touch and Touch.Parent then
06 
07        if Touch.Parent:FindFirstChild("Humanoid") then
08 
09            local Player = game:GetService("Players"):GetPlayerFromCharacter(Touch.Parent)
10 
11            if Player then
12 
13                local Backpack = Player:WaitForChild("Backpack")
14 
15                local Character = Touch.Parent or Player.Character
View all 27 lines...

Answer this question