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 sound occour when someone walks through an invisible block?

Recently, I have been developing a horror game, where the player must escape from a haunted facility. I would like to add sounds, when the player walks through an invisible wall, or part. How could I do it?

(BTW, I'm new to scripting...)

3 answers

This script works when a player touches the object. Please place the Sound object inside the part you put the script in. I also was nice and made it that the script preloads the sound for you.

sound = script.Sound
debounce = false
Game:GetService("ContentProvider"):Preload("http://www.roblox.com/asset/?id=" .. tostring(sound.SoundId))

script.Parent.Touched:connect(function(hit)
	humanoid = hit.Parent:findFirstChild("Humanoid")
	if(humanoid ~= nil and debounce == false) then
		debounce = true
		sound:Play()
		debounce = false
	end
end)

1) Create variables for SoundID, Block and a Debounce (for the touched event).

2) Preload the sound.

3) Create a Touched event on the Block.

4) Check if its Parent has a humanoid (Check if it's a player).

5) Create the sound from SoundID and play it.

Here is what it should look like:

local SoundID = 1234567
local Block = script.Parent
local Debounce = false

Game:GetService("ContentProvider"):Preload("http://www.roblox.com/asset/?id=" .. SoundID)

wait(1/30)

Block.Touched:connect(function(Hit)
	if Debounce == false then
		Debounce = true
		local Hum = Hit.Parent:findFirstChild("Humanoid")
		if Hum then
			local Sound = Instance.new("Sound", Workspace)
			Sound.Volume = 1
			Sound.SoundId = "http://www.roblox.com/asset/?id=" .. SoundID
			Sound.Looped = false
			Sound.Pitch = 1
			Sound.Archivable = false
			Sound:Play()
		end
	end
end)

1 comment

0
  1. script.Parent.Touched:connect(function(Part) 2.local Sound = Instance.new("Sound", workspace) 3.Sound:Play() 4.wait(5) 5.Sound:Stop() end
  1. Insert a sound into your brick and put the sound id in the properties "SoundID"
  2. Insert a script.
  3. Put this script in.
  4. Change the wait() to the number you want.

It's a very simple script.

script.Parent.Touched:connect(function(Part) local Sound = Instance.new("Sound", workspace) Sound:Play() wait(5) Sound:Stop() end

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