Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by
Jumbuu 110
4 years ago

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

4 answers

Log in to vote
2
Answered by 4 years ago

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.

Any questions? Post a comment?

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

Ad
Log in to vote
2
Answered by 4 years ago

I'm going to branch off of the other answers and give multiple ways only with their pros and cons.


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
The way I put the function right after the event is called and anonymous function. The event gives us the part that touched the part the script's inside.

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
This could also give us the player along with the character which can sometimes be useful.
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!

Log in to vote
1
Answered by
Goulstem 8139 Badge of Merit Moderation Voter Administrator Community Moderator
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.

There are two ways I can suggest to you for how to go about this.


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)
Log in to vote
0
Answered by 4 years ago

If you do via hitting a brick, I believe this would be the script

script.Parent.Touched:connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
local player = game.Players:FindFirstChild(hit.Parent.Name)
end
end)

Answer this question