script.Parent.Touched:Connect(function(player) print(player.Name) end)
I would like the script to print the name and not the bodypart!
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)
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.
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.
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.
: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)
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.
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