Does :GetTouchingParts() not get the character?

Hi, :grinning_face_with_smiling_eyes: im making a script in which if u touch a part, a function gets executed.
Now, ive used :GetTouchingParts to make the script more stable and accurate.
Here is my code for the script.

while true do
    wait()
    local Touch = PreviewButton:GetTouchingParts()
    local Character1 = nil				
    for i,v in pairs(Touch) do
        print(v)
        if v.Parent:FindFirstChild("Humanoid") then
	        Character1 = v.Parent
	    elseif v.Parent.Parent:FindFirstChild("Humanoid") then
	        Character1 = v.Parent.Parent
	    end
    end
end

and for some reason the PreviewButton does not detect the player’s parts as u can see here.

https://gyazo.com/34a092efa7cc2cd3ea2a16f013f8ce6f

It only detects the button that is situated in middle of the PreviewButton.
Is there anything where :GetTouchingParts() doesn’t detect character parts? I dont think ive disabled my characters can_collide.
:confused:

Am i doing something something wrong or :GetTouchingParts() does not work on the Character parts?
Im really confused here so it would be awesome if someone would help me. :pleading_face: :sob:

Regards, SpacePyro

1 Like

I would not recommend you to use the GetTouchingParts method instead of an event like Touched. Events are more efficient, cause they only fire a callback when necessary. Also, this code does not refresh quickly enough. Wait() only waits for about 1 / 20 of a second or so, while the physics engine refreshes at more than 60 FPS I believe.

This is probably how I would try to solve your problem:

local character = script.Parent

for _, descendant in pairs(character:GetDescendants()) do
   -- Check if the descendant is a part
   if descendant:IsA('BasePart') then
      -- Set an event listener
      descendant.Touched:Connect(function(part)
         if not part:IsDescendantOf(character) then
             -- Print every part that is not part of your character
             print(part)
         end
      end)
   end
end

Additionally, you could also stop the event connections like this:

local character = script.Parent
local touchedConnections = {}

for _, descendant in pairs(character:GetDescendants()) do
   -- Check if the descendant is a part
   if descendant:IsA('BasePart') then
      -- Set an event listener
      local connection = descendant.Touched:Connect(function(part)
         if not part:IsDescendantOf(character) then
             -- Print every part that is not part of your character
             print(part)
         end
      end)

      table.insert(touchedConnections, connection)
   end
end

-- For stopping the connected Touched events
local function disconnect()
   for _, connection in pairs(touchedConnections) do
      connection:Disconnect()
   end
end
3 Likes

Hi!

@Brickman808 is correct, .Touched events are more reliable than they might seem, despite the fact that they might not fire when it comes to really high velocities, for example when we detect bullet hits and very fast cars.

:GetTouchingParts() method doesn’t return characters, because it only retuns parts that are intersecting when CanCollide is turned on. There is, although, a tricky way to detect non-collidable parts, although I wouldn’t recommend this method.

You can even further simplify the process. No need to connect touch detection on every part, but rather have a server script connect .Touched and .TouchEnded.

local box = script.Parent
box.CanCollide = false; box.Anchored = true

box.Touched:Connect(function(object)
	if (object.Name == "HumanoidRootPart") then
		print(object.Parent.Name .." is inside the box!")
	end	
end)

box.TouchEnded:Connect(function(object)
	if (object.Name == "HumanoidRootPart") then
		print(object.Parent.Name .." left the box.")
	end
end)

Once the player is inside, .Touched event doesn’t fire anymore.

EDIT @pirospacepride1

Here is the link to :GetTouchingParts() API: BasePart:GetTouchingParts.

@Brickman808 HAPPY BIRTHDAY! :blush:

3 Likes

I forgot that he was doing this for a button lol.

2 Likes

But why is the :GetTouchedParts not working? I did use the .Touched() event and the .TouchEnded() event too but it did a lot of weird stuff like when i touched it and then jumped or something the touch ended event fired which isn’t supposed to happen and sometimes the .TouchEnded didn’t even fore so there was a thing there even though it shouldn’t have been.

Please let me know if u know why the :GetTouchedParts() didn’t work because the touched(0 events for me are not reliable. Also happy birthday lol.

The part in the gif looks like cancollides is turned off. In this scenario a special trick is needed for it to detect collisions via physics.

2 Likes

The events should be reliable though. What code have you used? Also, it is not my actual birthday, but thanks nonetheless!

1 Like

I told u, sometimes the .TouchEnded() event does not fire and i really want it to. Also sometimes even when im in the part and im touching it, the part that i am turning visible is not turning visible. So i would really like a solution for the :GetTouchingParts() so i can do it easily.