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...)
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.
01 | sound = script.Sound |
02 | debounce = false |
03 | Game:GetService( "ContentProvider" ):Preload( "http://www.roblox.com/asset/?id=" .. tostring (sound.SoundId)) |
04 |
05 | script.Parent.Touched:connect( function (hit) |
06 | humanoid = hit.Parent:findFirstChild( "Humanoid" ) |
07 | if (humanoid ~ = nil and debounce = = false ) then |
08 | debounce = true |
09 | sound:Play() |
10 | debounce = false |
11 | end |
12 | 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:
01 | local SoundID = 1234567 |
02 | local Block = script.Parent |
03 | local Debounce = false |
04 |
05 | Game:GetService( "ContentProvider" ):Preload( "http://www.roblox.com/asset/?id=" .. SoundID) |
06 |
07 | wait( 1 / 30 ) |
08 |
09 | Block.Touched:connect( function (Hit) |
10 | if Debounce = = false then |
11 | Debounce = true |
12 | local Hum = Hit.Parent:findFirstChild( "Humanoid" ) |
13 | if Hum then |
14 | local Sound = Instance.new( "Sound" , Workspace) |
15 | Sound.Volume = 1 |
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