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 3 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

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!

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 — 3y

3 answers

Log in to vote
0
Answered by 3 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 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
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 — 3y
Log in to vote
0
Answered by 3 years ago

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
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 — 3y

Answer this question