Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a part in a player detect if it's came in contact with another player?

Although this seems like I'm just missing something simple I'm having issues having players being able to detect if they have came in contact with another player. So I made this script and it's not working for whatever reason. Please help.

script.Parent.Touched:Connect(function(hit)
   local Human = hit.Parent:FindFirstChild("Humanoid")
      if Human ~= nil then
      print("Two players have touched!")
   end
end)

4 comments

0

GetPlayerFromCharacter() on the part of the player that hits another player

0

Is this under a part of the character model?

0

if something has a humanoid, it isn't always a player

0

@SerpentineKing yes it's a script placed inside of a part that I have welded to the player.

1 answer

✓ Accepted answer

Explanation

So, my assumption is that the part that you've welded to the player isn't being touched, and this is why the function isn't working. However, you don't even require the additional welded part.

If we use the CharacterAdded event of the Player Instance (taken from the PlayerAdded event of the Players Service) then we can get the player's character model.

Next, we can use the AncestryChanged event of the model to check that the character is parented under the workspace (which means its parts like the head and humanoidrootpart will have loaded in)

Then we get a table of all BaseParts under the character model and give each of them a .Touched event

Finally, we check that the part is touching a Humanoid with the :FindFirstChild("Humanoid") of what we presume will be a player character model, as well as the :GetPlayerFromCharacter() function of the Players Service to determine that we are not touching a NPC.

Now we can print our statement or perform a function.

Revised Server Script under ServerScriptService

game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(character)
		character.AncestryChanged:Connect(function(child, parent)
			if parent == workspace then
				for i, v in pairs(character:GetDescendants()) do
					if v:IsA("BasePart") then
						v.Touched:Connect(function(hit)
							if hit.Parent:FindFirstChild("Humanoid") and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
								print("Two Players Are Touching!")
							end
						end)
					end
				end
			end
		end)
	end)
end)

As you probably already know, you can also add a debounce to prevent further prints for a certain time period.

2 comments

0

What would I use to define the player that's being touched (So I can advance the script) would I do something along the line of "player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)"?

0

yep, you can use that to retrieve the player, I implemented it to check that you were touching another player, but you could also make it a local variable and use it after the check as well

This discussion is preserved as it was. New answers, comments, and votes are closed.