Play a sound when touching a part

1: the script below unanchors all parts of a group and emits smoke when touched. is there a way it can also make a sound when touched? and what will be the locations. 2: can there be a cooldown on the smoke emitter and sound

local function emitSmoke()
	for i, v in pairs(script.Parent.Parent:GetChildren()) do
		v.Anchored = false
		local smoke = Instance.new("Smoke")
		smoke.Parent = v
		smoke.Color = Color3.new(0, 0, 0)
		smoke.Opacity = 0.15
		smoke.RiseVelocity = 4
	end
	
	wait(2) -- Amount of till making smoke dissapear
	
	for i, v in pairs(script.Parent.Parent:GetChildren()) do
		v.Smoke:Destroy()
	end
end
script.Parent.Touched:Connect(emitSmoke)
1 Like

You should consult this article:

This will allow you to play sounds with just a few lines of code, if you place it in your script at the correct place it will make a sound.

Hope this helps,
-Tom :slight_smile:

3 Likes

this script should have a cooldown:

local ready = true
local sound = script.Parent.Sound--Change to your sound location.
local function emitSmoke()
if ready == true then
	ready = false
	for i, v in pairs(script.Parent.Parent:GetChildren()) do
		v.Anchored = false
		local smoke = Instance.new("Smoke")
		smoke.Parent = v
		smoke.Color = Color3.new(0, 0, 0)
		smoke.Opacity = 0.15
		smoke.RiseVelocity = 4
	end
	sound:Stop()
	sound:Play()
	
	wait(2) -- Amount of till making smoke dissapear
	
	for i, v in pairs(script.Parent.Parent:GetChildren()) do
		v.Smoke:Destroy()
	end
	ready = true
end
end
script.Parent.Touched:Connect(emitSmoke)
3 Likes
    local SP = script.Parent

 function onTouch (hit)
local H = hit.Parent : FindFirstChild("Humanoid")
if H ~= nil then
SP.S:Play()
end
end


script.Parent.Touched:connect(onTouch)
5 Likes

cooldown of both smoke and sound? and what are the locations? (of soundpart in workspace)

1 Like

it doesnt matter. such as if you put the sound under the part that the script is in it would be script.Parent.Sound

1 Like

where do i change the cooldown of smoke and sound?

its already set. its the if statement (the 1st line of the function)

So exactly how long is the cool down?

its until the smoke is finished, you can increase if you need

yes, i added a wait(), also, when i touch the part it doesnt unanchor, play a sound nor emit smoke anymore. no errors in output? whats wrong here

may I ask where you put the wait()?

it doesnt really matter because i deleted it and it still doesnt work but i put it in front of the local ready = true

local scripts dont work in workspace if you didnt know (if you’re using a local script).

its a normal script inside a part inside a model

does the model unanchor still?

nope, the script completely doesnt work anymore

then it’d be much easier to do this:

local ready = true
local sound = script.Parent.Sound--Change to your sound location.
script.Parent.Touched:Connect(function()
if ready == true then
	ready = false
	for i, v in pairs(script.Parent.Parent:GetChildren()) do
		v.Anchored = false
		local smoke = Instance.new("Smoke")
		smoke.Parent = v
		smoke.Color = Color3.new(0, 0, 0)
		smoke.Opacity = 0.15
		smoke.RiseVelocity = 4
	end
	sound:Stop()
	sound:Play()
	
	wait(2) -- Amount of till making smoke dissapear
	
	for i, v in pairs(script.Parent.Parent:GetChildren()) do
		v.Smoke:Destroy()
	end
	ready = true
end
end)

still doesnt work… i really dont know why

Here, I’ll test out inside of studio I’ll get back to you once finished.