Parts making sounds after hitting ground

Is there any way to make a script that plays a sound whenever it falls from a certain height or gets flung into walls or other parts?

Like if I were to throw an object across the room and it would hit a wall, how would I make a sound play on impact?

Side note: the touched function does not work very well.

runservice.heartbeat

detect before velocity and current velocity
subtract

if the velocity is now zero after it was 100, we know it hit something

play sfx

local before velocity = 0

hearbeat connect
local current veloctiy = part velocity
if current velocity - before velocity >= 30 then
pay sound it hit
end

before velocity = current velocity
end

that’s an example

this is the most reliable and precise method

haven’t tested it myself but I would solve it like this:

Create a part, name it hitbox and use a RigidConstrant to bind the thrown part and the hitbox.

Make the hitbox the same shape as the part and make it CanCollide false

Use a loop to constantly check if there’s any parts (Beyond the thrown item) within this hitbox.

If there is a part within this hitbox (and if you want you could also check if the linear assembly velocity drops by more than 10% or what ever value), fire the sound

Make the hitbox smaller for more accurate sounds, run the code on the client so that the physics doesn’t cause lag

You could raycast towards the part’s velocity in a Heartbeat loop in order to detect collision.

local RunService = game:GetService("RunService")

local targetPart = script.Parent
local raycastParams = RaycastParams.new()

raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {targetPart}

local LARGEST_AXIS_SIZE = math.max(targetPart.Size.X, targetPart.Size.Y, targetPart.Size.Z)

local updateConnection = nil
updateConnection = RunService.Heartbeat:Connect(function()
	local velocityRaycast = workspace:Raycast(targetPart.Position, targetPart.AssemblyLinearVelocity.Unit * LARGEST_AXIS_SIZE)
	
	if not velocityRaycast then
		return
	end
	
	updateConnection:Disconnect()
	-- Handle playing sound.
	print("Hit something")
end)