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