Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a part make a sound when a tool touches the object?

I tried adding a sound in the object, but that didnt work

1 comment

0

Can you give us a script, an image or video of your attempt, extra information about your explorer (like how your objects are arranged in your game, like the hierarchy) or ANYTHING? Don't just tell us that you put a sound in a part and that is it. Give us more information!

1 answer

First of all, you need to make sure that you are making your own progress on this and then asking for help on it if you can't manage to continue.

I'll let you slide on this one though.

Firstly you want the sound to exist so you're going to need to find your sound file and place it in ReplicatedStorage let's name it "TouchSound".

Also let's place this script inside of the part that you want to make a sound.

Let's define them.

local Sound = game.ReplicatedStorage:FindFirstChild("TouchSound")
local Part = script.Parent

Now that we have everything defined we can start our function.

Part.Touched:Connect(function(hit)
    If hit.Parent:FindFirstChild("Humanoid") ~= nil then

Here we're trying to figure out that the person that hit the part is actually a player so that something like an unanchored part or a dropped tool can't activate it.

Now check how long your sound is so we can stop it at the right time.

local SoundTime = 5 --change this number to your time

Let's make our sound start and stop.

Part.Touched:Connect(function(hit)
    If hit.Parent:FindFirstChild("Humanoid") ~= nil then
         Sound:Play()
         Wait(SoundTime)
         Sound:Stop()

Now let's add a debounce in order for it to not play a numerous and uneccessary amount of times.

local deb = false

Part.Touched:Connect(function(hit)
    If deb == true then return end
    deb = true
    If hit.Parent:FindFirstChild("Humanoid") ~= nil then
         Sound:Play()
         Wait(SoundTime)
         Sound:Stop()
         Wait(0.1)
         deb = false
    end
end)

Now let's put our script together!

local Sound = game.ReplicatedStorage:FindFirstChild("TouchSound")
local Part = script.Parent
local SoundTime = 5 --change this number to your time
local deb = false

Part.Touched:Connect(function(hit)
    If deb == true then return end
    deb = true
    If hit.Parent:FindFirstChild("Humanoid") ~= nil then
         Sound:Play()
         Wait(SoundTime)
         Sound:Stop()
         Wait(0.1)
         deb = false
    end
end)

Thank you if it doesn't work tell me what's not working I wrote this on my phone.

This discussion is preserved as it was. New answers, comments, and votes are closed.