Im trying to make a sound play when a ragdoll hits or collides with something for my ragdoll game. Any ways on hot to do it?
You can use .Touched
events and connect it to play a sound.
local ragdoll = workspace:WaitForChild("Ragdoll") --change to name of ragdoll
local sound = workspace:WaitForChild("Sound") --change to name of sound
for i, part in pairs(ragdoll:GetDescendants()) do
if part:IsA("BasePart") then
part.Touched:Connect(function(hit)
sound:Play()
end)
end
end
Here’s a relatively simple template, it’ll connect every part of the ragdoll to a “Touched” event which when fired will execute a function which plays a sound (make sure you change the names of the sound and ragdoll and references if necessary).
Is there a way for it to work with a player only when they are ragdolled?
if Humanoid:GetState() == Enum.HumanoidStateType.Ragdoll then
Make sure “Humanoid” is correctly defined.
When I use the script, it works but the sound glitches out.
That’s because the “Touched” event is firing multiple times, add a debounce to prevent that:
local ragdoll = workspace:WaitForChild("Ragdoll") --change to name of ragdoll
local sound = workspace:WaitForChild("Sound") --change to name of sound
local debounce = false
for i, part in pairs(ragdoll:GetDescendants()) do
if part:IsA("BasePart") then
if debounce then
return
end
debounce = true
part.Touched:Connect(function(hit)
sound:Play()
end)
task.wait(3)
debounce = false
end
end
Change the time in seconds from “3” if necessary.
This script is from a free model I found on Roblox and it works well:
local air = false
local imp = false
script.Parent.Touched:connect(function(h)
if not h:IsDescendantOf(script.Parent.Parent) and air == true and imp == false and h and h.Transparency<1 and script.Parent.Velocity.Magnitude > 0.5 then
air = false
imp = true
local sou = math.random(1, #script:GetChildren())
local s = script["Impact"..sou]:clone()
s.Parent = script.Parent
s.Name = "Impact"
game.Debris:AddItem(s, 3)
s:Play()
end
end)
while true do
wait()
local ray = Ray.new(script.Parent.Position,Vector3.new(0,-2,0))
local h,p = game.Workspace:FindPartOnRay(ray,script.Parent.Parent)
if h then
else
air = true
imp = false
end
end
Put the impact sounds you’d like in the script and name them “Impact” with a count so for example:
“Impact1”
“Impact2”
“Impact3”
“Impact…”
Put the script inside of the limbs which you want to make a sound.
After that, you should be good to go!