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