Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how can i make a sound play when a part is touched?

Asked by 6 years ago

hi, im trying to make water which kills you and i need it to play a sound effect when it's touched.

this is the script i came up with:

function onTouch(part)
    script.Parent:Play()
end

thanks in advance, from poopypigeon245

0
script is a child of the sound poopypigeon245 22 — 6y
0
Not sure if this'll work, but try it: Pianotech 48 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
function onTouch (part)
    script.Parent:Play()
end
script.Parent.Touched:connect(onTouch)
0
What you did wrong was that you weren't connecting the function. You were just saying what the function does. The last line of code basically connects the function 'onTouch' to script.Parent.Touched (when part is touched). Florian27 76 — 6y
0
it didnt work poopypigeon245 22 — 6y
0
Oops. Change the last line (script.Parent) to wherever your part is located. Florian27 76 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Well, the function is not connected. You can try the following:

local CanPlay = true -- To prevent the song from playing multiple times.
local CanKill = true

function Play(hit)
    local Song = workspace.YourSong
    if hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("Humanoid") then -- We check for a humanoid and if the hit's parent is a Model.
        if CanPlay and CanKill then -- If CanPlay and CanKill are true.
            hit.Parent:BreakJoints() -- We kill the Character.
            CanPlay = false 
            CanKill = false -- We set both variables to false.
            Song:Play() -- We play the song.
            wait(5) -- 5 Second delay.
            CanPlay = true 
            CanKill = true -- We set both variables to true.
        end
    end
end

script.Parent.Touched:connect(Play) -- In my case, I parent the script to the Part.

Hope that helped. (Sorry for any mistakes, grammar mistakes or bad explanation.)

-AlessandroG200

Answer this question