How can a touched:connect(Hit) function ignore players or certain parts?

Please Help! I’m about to give Up on this. Or How do you Prevent a part from getting Touched in the first place?

Will collision filters work with touched functions on can collide false parts?

local Part = script.Parent

local ignoretable = {"Humanoid"}

Part.Touched:connect(function(hit)
    if hit.ignoretable then
    else
        ---- code
    end
end)
5 Likes

CanCollide parts and CollisionGroups only affect physics.
Your best bet is to check if an instance is inside a table and then ignore it.
hth!

1 Like

how would I add (Playeradded) and (Charateradded) to the table?

What do you mean by adding an event to a table?

1 Like

sorry for the vague description, I mean when a player joins or dies how do i add their character to the table?

2 Likes

If you’re checking whether players will collide or not, you can simply check if the touched part is a parent of any character.

1 Like

I would like it if the Player did not even collide with the part because i would not have to check for that player

Even if the player cannot collide with the part at all, the part will still detect the player there.
You can do:

script.Parent.Touched:connect(function(oP)
    if not oP:FindFirstChild("Humanoid") then
        -- Do something here
    end
end)
2 Likes

how do i add a video to show you what is happening?

You can use Gyazo or record using the in-studio recorder.

1 Like

i think this might work :confused: i did it though my brother YT accout

Can you display the whole script?
Correct me if I’m wrong, but you’re trying to stop it from flashing?

1 Like

Yeah, I tried to shorten the script to be easier to read.

local part = script.Parent

debounce = true
	
function onTouch(part)	-------detects bubble touched
	if part.Parent:WaitForChild("Bubble") then
		print(part,"Touched")
		debounce = false
	end
end



function onTouchEnded(part) ------ detects bubble touchEnded
	if part.Parent:FindFirstChild("Bubble") then
		print(part,"Touched Ended")
		debounce = true
	end
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)

	
while true do ----- this does not matter
	if debounce then
		part.BrickColor = BrickColor.Random()
	end
	wait()
end

Use :GetTouchingParts() and check to see if the bubble is touching the brick rather than :TouchEnded() as that is really unreliable.

1 Like

Can I use a Print() to see the results with GetTouchingParts()?

You’ll need to iterate through it with a pairs loop.

1 Like

can I do

if GetTouchingParts("Bubble").TouchEnded() then
      ----- code
end

Try switching your onTouch event function to FindFirstChild instead of WaitForChild.

If you only want the bubble to affect it, maybe something like this

function onTouch(part)	-------detects bubble touched
	if part.Name=="Bubble" then
		debounce = false
	end
end
1 Like

The best way is to do:

for _, i in pairs(part:GetTouchingParts()) do
    if i.Name = "Bubble" then
        -- Do something
    end
end

Stick that in a loop.

8 Likes

^ that works too, but it kinda just throws away the event structure

2 Likes