Hello! So I have this script to where when I click a certain part, that part that I click will be destroyed, and I will have the tool in my inventory. What I am needing is from it to be changed from being a click detector script, to an on touched script. Where instead of it being clicked, all I need is for it to be touched. I want the same stuff to happen, just changing on how to obtain the tool. Here is the script:
script.Parent.ClickDetector.MouseClick:connect(function(plr) if plr.Backpack:FindFirstChild("Brick") then else local clone = game.Lighting.Brick:Clone() clone.Parent = plr.Backpack script.Parent:Destroy() end end)
Make the event 'script.Parent.Touched'.
The part being touched is whatever part the click detector is inside of.
The hit
variable inside the touch event is the part that is currently touching the part we just mentioned.
If the player touches the part, then the the player's foot, arm, head, etc is the hit variable. Since the leg/arm/foot etc is inside the player's character, hit.Parent.Name
will give us the player's name. if we want to get the player object we'd say:
game:GetService("Players"):FindFirstChild(hit.Parent.Name)
The whole code would be:
script.Parent.Touched:Connect(function(hit) local player = game:GetService("Players"):FindFirstChild(hit.Parent.Name) --if the player exists, run the following code if(player)then if player.Backpack:FindFirstChild("Brick") then --TODO: DO SOMETHING HERE else local clone = game:GetService("Lighting").Brick:Clone() clone.Parent = player.Backpack script.Parent:Destroy() end end end)