Best way to detect player collisions?

Hey :wave:

I’m trying to find a good way to detect collisions between players’ character models. In a group vs. group of players scenario, I want to be able to catch when a player’s model hits another player’s model, determine if they are on the same team and if not then fire some code. What’s a good/efficient way of going about this? I don’t want to cycle through the player’s character models to repeatedly run code to check. I thought maybe collision groups could work but I don’t know if that would even work/how I would detect a collision.

Any ideas or solutions would be greatly appreciated. Or even if the solution is blatantly obvious and I’ve missed it :wink:

3 Likes

you could just use BasePart.Touched on the HumanoidRootPart

If that doesn’t work, you could create a transparent colliding part that is sized to fit the entire character and weld it to the HumanoidRootPart.

Something along the lines of this:

function addBody(body)
	body:WaitForChild('HumanoidRootPart').Touched:connect(function(hit)
		local otherPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
		if otherPlayer then
			print(body.Name..' and '..otherPlayer.Name..' collided!'
		end
	end)
end

game.Players.PlayerAdded:connect(function(newPlayer)
	newPlayer.CharacterAdded:connect(addBody)
end)
7 Likes

This method would be good, i personally recommend the hitbox option with the transparent part, just put a part that is roughly the size of their character model and use that to detect collisions with the touched event. This method looks a little better than the HumanoidRootPart method as it can also account for Limbs such as a player jumping on another players head.

2 Likes

@dispeller @kinkocat

Thanks! That suits what I was trying to achieve and I think I’ll also incorporate the hitbox to catch if the limbs are touched too. Appreciate your help :slight_smile:

3 Likes