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

How do I make a part disappear only after being touched by a certain object?

Asked by 6 years ago

The script below makes the part disappear after being touched by anything, which is a problem. But I don't know how I will make it so it will only disappear after being touched by a certain object.

 health = script.Parent.Parent.Parent.Health
local debounce = false

function onHit()
if debounce == false then
    debounce = true
    health.Value = health.Value - 10
    print (health.Value)
    wait(3)
    debounce = false
    script.Parent:Destroy()
    end
end

script.Parent.Touched:connect(onHit)

1 answer

Log in to vote
0
Answered by
Vulkarin 581 Moderation Voter
6 years ago
Edited 6 years ago

Touched has an optional argument to see what touched it, for example if you wanted to see if the toucher had a Humanoid inside

function onHit(toucher)
    if toucher.Parent:FindFirstChild("Humanoid") then
        print("This part was touched by something with a humanoid inside")
    end

    -- or you could filter by name, etc
    if toucher.Parent.Name == "someSpecialPart" or toucher.Name == "someSpecialPart" then
        print("This part was touched by someSpecialPart")
    end
end

script.Parent.Touched:Connect(onHit)
Ad

Answer this question