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

How would you make an on touch script only activate when a humanoid touches it (and not any part)?

Asked by 5 years ago

Sorry for the spam.

I am making a vent that unanchors itself when it is touched, but the script automatically enables itself because it is touching the surrounding walls. Here's what I have so far:

fall = script.Parent
fall.Touched:Connect(function()
    fall.Transparency = 0
    fall.CanCollide = true
    for _, object in pairs(fall.Parent:GetChildren()) do
    object.Anchored = false
end
end)

How would you make it so that the union only unanchors when a player or humanoid touches it and not all parts? Hopefully that made sense.

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

If you want to check if the thing that touches it is any humanoid, you can use FindFirstChildOfClass to see if there is a Humanoid inside the parent of the thing that touched it:

script.Parent.Touched:Connect(function(Hit)
    if Hit.Parent:FindFirstChildOfClass("Humanoid") then
        print("Touched by a part from a model with a humanoid inside!")
    end
end)

However, if you only wanted to check if the thing that touched was a character of a player, you can use GetPlayerFromCharacter, which will return nil if there is no player that owns the model:

script.Parent.Touched:Connect(function(Hit)
    local Plr = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if Plr then
        print("Touched by " .. Plr.Name)
    end
end)

Hope this helps!

0
This isn't an answer, but I vaguely remember trying to make a button and putting Humanoid into a parenthesis. It made the button only work when stepped on by a humanoid. Beginner's luck, I guess. kofe_i 4 — 5y
Ad

Answer this question