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

How do I find a the player that touched a brick?

Asked by 5 years ago
script.Parent.Touched:Connect(function(player)
    print(player.Name)
end)

I would like the script to print the name and not the bodypart!

3 answers

Log in to vote
1
Answered by 5 years ago

Hi tightanfall! I'm here to help you!

To find who have touch the brick, first use Players service and :GetPlayerFromCharacter()

script.Parent.Touched:Connect(function(hit)
    local player = game:GetService('Players'):GetPlayerFromCharacter(hit.Parent)
    if player then
        print(player.Name)
    end
end)
0
The only thing missing from this answer is to detect if the hit’s parent is actually the character. Otherwise, this answer is correct saSlol2436 716 — 5y
0
^ No. User#24403 69 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

What is the argument of the Touched event?

The argument of the Touched event is the Part that touched the brick. In addition, the body part is in the character, not player. People usually use hit as the name of this argument.

So, how do we check if the body part is in the character?

We can use hit.Parent to find out the parent of hit. We can use the :FindFirstChild method to check if there is a Humanoid. We use this method instead of usual dot notation(Model.ModelChild) because when we check if it’s nil and if it is true, it will error. This method will hide that error.

How do we get the player from the character?

The answer to this question is very simple. We can use the :GetPlayerFromCharacter method from the Players service. This method obviously uses the Character to get the Player.

Final Code:

:connect is deprecated, or not recommended to use. Use :Connect *I used a phone to type this, so things (such as comments) won’t work)

script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild(“Humanoid”)
        if not humanoid then return end  —I will explain what this is later if you don’t know.

    local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
    print(player.Name)
end)

if not humanoid then return end

What does this mean? First we must understand what not humanoid means.

We know that if the humanoid does not exist, it is a falsey value(false and nil return a falsey value)

We also know that an if statement works only when we have a true condition.

not simply reverses a boolean(thus, not true = false and not false = true)

return simply returns something. However, in this case, it is used to stop the function

If you have any questions, don’t hesitate to ask.

1
Very well formatted answer. EpicMetatableMoment 1444 — 5y
Log in to vote
0
Answered by 3 years ago

There is actually a simpler version of this answer you just have to do this

script.Parent.Touched:Connect(function(player)
    print(player.Parent.Name)
end)

When you add the parent it will show you the parent of the body parts which is always gonna be the username

Answer this question