Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

01function (Touched)
02 
03if ThePartThatTouched = Game.Workspace.Map then
04 print ("Map")
05else
06 print ("else")
07  if ThePartThatTouched = NameOfTheSphereIUsedInTheDummy
08   ThePartThatTouched:FindFirstChild("Humanoid").Health = 0
09  end
10        end
11 
12script.Parent.TouchesAnotherPart (Touched)

Thank you very much for reading!

0
why not add a script in the part that kills the player if it touches it make like a hitbox on the player DuckyRobIox 280 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago

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.

0
Yea, I probably could do that but the main question im asking is how to detect if a part is touching another wegetab 9 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Use the Part.onTouched event.

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

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.

1YourPart.Touched:Connect(function(thePartThatHit)
2    local humanoid = thePartThatHit.Parent:FindFirstChildOfClass("Humanoid")
3 
4    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
5        humanoid.Health = 0 -- Kills the humanoid
6    end
7 
8end)
0
I have now tried doing the, YourPart.Touched:Connect(function(thePartThatHit) But that still doesnt seem to detecting anything was actually touching it. wegetab 9 — 4y
Log in to vote
0
Answered by 4 years ago

Use the part:GetTouchingParts() function.

For example:

1local Part = script.Parent
2local 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.
3local TouchingParts = part:GetTouchingParts()
4for key, TouchingPart in pairs (TouchingParts) do
5    TouchingPart.Transparency = 1 --Do whatever you want to the TouchingPart variable to affect all parts touching the Part.
6end
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. wegetab 9 — 4y

Answer this question