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

How i do make a script on a part that if you touch it that removes your tools?

Asked by 8 years ago

I want to make a script,Thats remove your tools while touch a part/brick,How i do make it,Its for someone i need it to.For whos awansered thanks for helping :)

2 answers

Log in to vote
0
Answered by 8 years ago

I'll comment you through the code.

script.Parent.Touched:connect(function(Touch)

    local Player = game.Players:GetPlayerFromCharacter(Touch.Parent)--Using the Parameter that I named "Touched" to access the Player
    local Tool = Player.Character:GetChildren()--Gets all the Children that are Inside of the Character
    local BackPack = Player.Backpack:GetChildren()--Gets All the Children that Are Inside of Player's Backpack

for _,v in pairs (Tool) do --This in pairs loop loops through the Character Children
    if v:IsA("Tool") then--Check if any of the Children are ClassNamed "Tool".
    v.Parent = Player.Backpack--Parent the Tool to the Player's Backpack. The reason why I parented to the Player's Backpack instead of Destroying it is because when the Player holds the Tool and the tool is removed the Arm stays in the position as if it was still holding the tool.
    end end 

for _,v in pairs (BackPack) do--This in pairs loop loops through the Player's Backpack
    if  v:IsA("Tool") then--Check if there Any Object className "Tool" in the backpack
    v:Destroy()-- if it is then its Destroyed here.
    end end

end)

If you want a further explanation about on of the things I wrote please feel free to comment below! If I helped you solve/answer you problem please accept my answer.

0
Hi. When I tested it out to see if it works, and because it looks cool, it had an error on line 4 saying: 16:14:57.057 - Workspace.RemoveToolsTouch.Remove:4: attempt to index local 'Players' (a nil value) RobotChitti 167 — 8y
Ad
Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 years ago

Hmm. Well, in your question, the word 'while' has me confused. Do you want it to remove your tools when you touch the brick, and it never comes back? Or it comes back once you step off the brick? I'll set up both situations.

Never comes back:

script.Parent.Touched:connect(function(hit)
    if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then
        local Player = game.Players:GetPlayerFromCharacter(hit.Parent) 
        for i,v in pairs(Player.Character:GetChildren()) do
            if v:IsA("Tool") or v:IsA("HopperBin") then
                v:Destroy()
            end
        end
        for i,v in pairs(Player.Backpack:GetChildren()) do
            if v:IsA("Tool") or v:IsA("HopperBin") then
                v:Destroy()
            end
        end
    end
end)

It comes back:

For this one, I recommend making a model Instance inside of the Player, to store all of the tools and HopperBins they had. Then, once they step off the brick, they get their tools back.

script.Parent.Touched:connect(function(hit)
    if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) and not game.Players:GetPlayerFromCharacter(hit.Parent):findFirstChild("Weapons") then
        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local Model = Instance.new("Model", Player)
        Model.Name = "Weapons"
        for i,v in pairs(Player.Character:GetChildren()) do
            if v:IsA("Tool") or v:IsA("HopperBin") then
                v.Parent =  Model
            end
        end
        for i,v in pairs(Player.Backpack:GetChildren()) do
            if v:IsA("Tool") or v:IsA("HopperBin") then
                v.Parent =  Model
            end
        end
    end
end)

script.Parent.TouchEnded:connect(function(unhit)
    if unhit.Parent and game.Players:GetPlayerFromCharacter(unhit.Parent) and game.Players:GetPlayerFromCharacter(unhit.Parent):findFirstChild("Weapons") then
        local Player = game.Players:GetPlayerFromCharacter(unhit.Parent)
        for i,v in pairs(Player.Weapons:GetChildren()) do
            v.Parent = Player.Backpack
        end
        Player.Weapons:Destroy()
    end
end)
0
I meant to all of my tools on backpack guyalf1 0 — 8y
0
@guyalf1 Well, you can edit it. This question is supposed to be locked, but I decided to answer it. I took the time to make this. If you want it changed, you change it Shawnyg 4330 — 8y

Answer this question