How could I add to this script to ignore touched events inside of the same model it's in?

Hi. I have a script that basically makes it easier to add sound effects to stuff for when it hits something hard. The script goes inside the part you want to emit noise for when it gets hit. Now, I have already achieved the main idea of the script, but I’m looking to solve the problem of sounds emitting from the limb even before it hits the ground because when in mid-air, it passes the velocity threshold and it’s colliding with a moving body part and making a sound. I want it to ignore all collisions within the player model. This is my script.

local soundTable =  script.Sounds:GetChildren() -- yooo automated

script.Parent.Touched:Connect(function()
	if script.Parent.Velocity.Magnitude >= 40 then
		local randomSound = soundTable[math.random(1, #soundTable)]
		local clonedSound = randomSound:Clone()
		clonedSound.Parent = script.Parent
		clonedSound:Play()
		wait(3)
		clonedSound:Destroy()
	end
end)

I have tried tinkering around with :GetDescendants() to get all of the parts within the model (Just script.Parent.Parent:GetDescendants()) however I cannot wrap my head around how to keep the code going in terms of the touch indicating part of the script.

1 Like

How about a debounce?
The Touched event is firing each time the Part hits anything, so another cloned sound it being made.
If you have 4 Parts that it touches, you are creating 4 sounds.
A simple debounce that’s the length of the sound should work.

1 Like

So you want to stop it from checking if the limbs of the player’s character are touching each other? Would something like this work?


local soundTable =  script.Sounds:GetChildren() 

script.Parent.Touched:Connect(function(hitPart)
    if hitPart.Parent ~= script.Parent then
        — your code here
    end
end)

Just makes sure that the parent of the hit part is not the same as the parent of the script (which i assume is the player character?)

1 Like

You can either make a blacklist, or make use of the Collision Filtering features of the PhysicsService.

1 Like

Thank you so much for the solution!!! However, it’s not “~= script.Parent”, it’s “== script.Parent.Parent”. Once again, big thanks!

2 Likes