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

How do I make a Touched script only trigger when touched by another player?

Asked by
DVDKO 20
8 years ago

I want my script to only function if a player touches another player, instead of all bricks. How do I do this?

2 answers

Log in to vote
0
Answered by 8 years ago

I'll explain what each line does by commenting!

script.Parent.Touched:Connect(function(Hit)--Touched Event calls the function each time a Object has touched the Part.
local Humanoid = Hit.Parent:FindFirstChild("Humanoid")--Using Parameter called "Hit" and FindFirstChild() to see if the object has a Child called "Humanoid"
if Humanoid then--If the Object has a Child called "Humanoid" then it will print:
print("Player has touched")
end)
Ad
Log in to vote
0
Answered by 8 years ago

You have to use an if statement to make sure that the brick you touched belongs to another player in the game. Now there are a few ways of checking if the brick belongs to a player. One way is looping through all the players characters and checking to see if the character is an ancestor of that part. (Doing this is better then just checking if the bricks parent is a character because hats parts are only descendants of the character and not actually children). Which can be done with the code below.

workspace.Part.Touched:connect(function(brick)
    for i, v in pairs(game.Players:GetPlayers()) do
        if v.Character:IsAncestorOf(brick) then
            --Brick is part of a character and you can now do whatever you want to him
        end
    end
end)

Answer this question