Still have questions? Join our Discord server and get real time help.
0

# How do i find a players character using the touched event?

Jumbuu 110
4 years ago

is there a special parameter i have to use? this sint a request, i just need some insight

2

When detecting a character you need to find the humanoid. You can find the humanoid by doing 'hit' in a functions parameter.

Example:

game.Workspace.Touched:connect(function(hit)
local b = hit.Parent:FindFirstChild("Humanoid")
if b ~= nil then
print("I'm touched!")
end
end)


example 2: (if you don't do anonymous functions)

function Touched(hit)
local b = hit.Parent:FindFirstChild("Humanoid")
if b ~= nil then
print("I'm touched!")
end
end

game.Workspace.Touched:connect(Touched)


This is just 1 one of the ways to find the players humanoid.

if anything is wrong, please tell me and I'll correct it

2

## Number One, Finding The Humanoid.

#### Normally if a part's parent has a Humanoid, it's a character. This method is simple, and works great. However, if you have NPCs are moving models with Humanoids that might touch the brick, things start to error. A method like this would look similar to the below.

-- regular script in a part.
script.Parent.Touched:connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then
print("A character touched me! It was "..part.Parent.Name)
end
end


## Number Two, Check if the part's parent is a player.

#### Roblox gives us a very useful function called GetPlayerFromCharacter. All we have to do is give the function the character and it gives us the player. If there in no player it returns nil. This method should never error, so it's very reliable. This is more code to write however. An example of this method would look somewhat like below.

-- regular script in a part.
script.Parent.Touched:connect(function(part)
if game.Players:GetPlayerFromCharacter("part.Parent") then
print("A character touched me! It was "..part.Parent.Name)
end
end

##### I was going to throw in some crappy methods but I didn't feel the need. There may, and probably are, tons more methods that I don't show above.

Hope I helped!

Good Luck!

1
Goulstem 8139
4 years ago
Edited 4 years ago

The Touched even returns 1 value. This value is the object that has touched!

part.Touched:connect(function(hit)


In the script above, 'hit' would be the object that touched 'part'.

Now, the thing is the actual "Character" object of the player is not the object that would potentially fire the touched event on 'part'. That object could only be the Right Arm, Left Arm, Right Leg, Left Leg, Head, or Torso components of the Character. So looking into anything about the 'hit' object does us no good.

1) Try to detect the Humanoid

The 'Humanoid' component is something that only Characters should contain. You can search 'hit.Parent' - the potential Character - using the FindFirstChild function. If the Humanoid is found, then hit.Parent is your Character.

part.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then --Check for humanoid
local character = hit.Parent --This is the Character
end
end)


2) Try to detect the Player

You can obtain the 'Player' object from the Character object using the GetPlayerFromCharacter function. With this function, you use the potential Character as parameters to call it. In this case that would be hit.Parent. So check to see if the function returns a player, and then you'll know that hit.Parent is your Character.

part.Touched:connect(function(hit)
--Get the player
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
--Check if it's real
if player ~= nil then
local character = hit.Parent --This is your Character
end
end)

0
script.Parent.Touched:connect(function(hit)