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

Unequip tool on touch brick?

Asked by 7 years ago

I'm not asking for a script or anything (although it would be nice), where would I look if I wanted to create a brick that unequips the tools you're holding when you touch it? Thanks!

0
I would look into the touched event on parts and note that tools are parented under the Character to equip them. You would then need to just move the tool from the character object back to the player's backpack. M39a9am3R 3210 — 7y
1
Basic code: part.Touched:Connect(function(hit) plr = game.Players:GetPlayerFromCharacter(hit.Parent) if plr then tool = GetToolInPlayer(plr) if tool then tool.Parent = plr.Backpack end end end) --You'll need to complete the touched logic and do the GetToolInPlayer logic yourself. M39a9am3R 3210 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

So to get started you want to check when the part gets hit, to do this you can do

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

end)

and the hit in between the () in front of the function will detect what hit it, so to detect if its a player you want to do

local human = hit.Parent:FindFirstChild("Humanoid")
if human then
 print("Its human")
end

So now we need to check if the player has the tool, so just the same as we did with the humanoid

local Tool = hit.Parent:FindFirstChild("ToolName")
 if Tool then
  print("Found tool")
end

so if it found the tool is should print Found tool, now we need to get the player and its backpack, to get the player just do

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
 if player then
 print("Found player!")
end

now that we have the player we need to find the backpack which is in the player and i you need to define the tool after the part where it finds the humanoif, to do so just add a simple line

local Backpack = player:FindFirstChild("Backpack")
 if Backpack then
  print("Found backpack")
end

now if it found the backpack we can insert the tool into the backpack by just simply saying

Tool.Parent = Backpack

and that should do it, here is all the code:

script.Parent.Touched:connect(function(hit)
local human = hit.Parent:FindFirstChild("Humanoid")
if human then
 print("Its human")
   local Tool = hit.Parent:FindFirstChild("ToolName")
     if Tool then
      print("Found tool")
     end
      local player = game.Players:GetPlayerFromCharacter(hit.Parent)
         if player then
           print("Found player!")
             local Backpack = player:FindFirstChild("Backpack")
               if Backpack then
                 print("Found backpack")
                  Tool.Parent = Backpack
               end
          end
      end
end)

So i really hope this helped you out, i do suggest checking out the wiki it can really help you learn scripting, Upvote and select as answer if it helped, comment on here or message me if you have and questions, user is EndorsedScripter

0
Didn't actually work but I'll accept your answer because you tried :) Aryosis 3 — 7y
0
I tested it with me and it worked KinqAustinn 293 — 7y
Ad

Answer this question