is there a special parameter i have to use? this sint a request, i just need some insight
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
-- 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
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
Hope I helped!
Good Luck!
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)
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)