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

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

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

2 comments

0

script is a child of the sound

0

Not sure if this'll work, but try it:

2 answers

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

3 comments

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

0

it didnt work

0

Oops. Change the last line (script.Parent) to wherever your part is located.

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

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