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

How do I know if a part is touching another part?

I am a pretty new coder making a ball game where the point is basically that, if you dash into another player/ball they get killed. So to make the ball I took an R6 dummy and inserted a sphere part. But when i try to use the regular Touched nothing happens. I was thinking maybe the part I used in the dummy doesnt count as a bodypart? Because I kind of just made it barely bigger than the dummy and made all the other body parts uncollidable and totally transparent.

So what I wanna do is kind of

function (Touched)

if ThePartThatTouched = Game.Workspace.Map then
 print ("Map")
else
 print ("else")
  if ThePartThatTouched = NameOfTheSphereIUsedInTheDummy
   ThePartThatTouched:FindFirstChild("Humanoid").Health = 0 
  end
        end 

script.Parent.TouchesAnotherPart (Touched)

Thank you very much for reading!

1 comment

0

why not add a script in the part that kills the player if it touches it make like a hitbox on the player

3 answers

You could name each sphere the same as a player's name and check if the part's name is that, if so, do stuff.

1 comment

0

Yea, I probably could do that but the main question im asking is how to detect if a part is touching another

Use the Part.onTouched event.

YourPart.Touched:Connect(function(thePartThatHit) -- When YourPart is touched, run this function. Register the touching part as a variable called thePartThatHit.
	
end)

If a character touches YourPart, for example, thePartThatHit would be a body part such as LeftArm, Head, Torso, etc. From here we can locate the humanoid, whose parent is the character model.

YourPart.Touched:Connect(function(thePartThatHit) 
	local humanoid = thePartThatHit.Parent:FindFirstChildOfClass("Humanoid")
	
	if humanoid then -- Confirming if indeed a Humanoid exists. If there isn't a Humanoid this variable will be nil (nonexistent), the part that touched is probably not a character. If it does, then continue
		humanoid.Health = 0 -- Kills the humanoid
	end

end)

1 comment

0

I have now tried doing the, YourPart.Touched:Connect(function(thePartThatHit) But that still doesnt seem to detecting anything was actually touching it.

Use the part:GetTouchingParts() function.

For example:

local Part = script.Parent
local connection = Part.Touched:Connect(function() end) --This allows it to detect parts touching it even if they are for example anchored and intersecting with the part.
local TouchingParts = part:GetTouchingParts()
for key, TouchingPart in pairs (TouchingParts) do
	TouchingPart.Transparency = 1 --Do whatever you want to the TouchingPart variable to affect all parts touching the Part.
end

1 comment

0

Ok, tried this its the same problem as with everything else. It doesnt think they are touching. There was also an error at line 3 because Part was missing an capital P. Thank you very much tho.

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