I am currently coding a door system. I’d like to implement sound effects for everybody to hear.
Unfortunately, whenever I touch the part, the door opens, but the Sound does not play.
I have a folder in ReplicatedStorage called Sounds, and I have a ‘OpenDoorEffect’
Sound object in the folder.
ReplicatedStorage>Sounds>OpenDoorSoundEffect
Code
I have a ServerScript in ServerScriptService, which contains the following code:
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local sounds = ReplicatedStorage.Sounds
local openDoorEffect = sounds.OpenDoorEffect
local doors = workspace.Doors
local doorTriggers = CollectionService:GetTagged("DoorTrigger")
for _, doorTrigger in ipairs(doorTriggers) do
local doorTriggerConnection
local function onDoorTriggerTouched(hit, character, player)
-- Retrieve 'Door' and a digit (i.e., 1)
local doorNumber = doorTrigger.Name:match("Door%d")
local doorObject = doors:FindFirstChild(doorNumber)
local tweenInfo = TweenInfo.new()
local updateVector = Vector3.new(5, 0, 0)
openDoorEffect:Play() -- this is the code that is not working
TweenService:Create(
doorObject.Left,
tweenInfo,
{CFrame = doorObject.Left.CFrame - updateVector}
):Play()
TweenService:Create(
doorObject.Right,
tweenInfo,
{CFrame = doorObject.Right.CFrame + updateVector}
):Play()
doorTriggerConnection:Disconnect()
end
doorTriggerConnection = doorTrigger.Touched:Connect(onDoorTriggerTouched)
end
More Information
The code DOES NOT error
Whenever I print openDoorEffect.IsPlaying, it prints true
The sound is probably playing but, it’s parented in replicated storage and has no physical location to play from, try putting the sounds in the workspace within a part, or inside the actual door.
I don’t really know if that is the problem since I have played sounds that are descendents of ReplicatedStorage. I’ll give it a try though
Edit: It worked. Although, I don’t feel comfortable to place Sounds in Workspace. It makes sense to put it in a Storage service, so I’ll wait for other solutions.
I see, that’s my bad for missing that. I can’t find any documentation that says Sounds play when in ReplicatedStorage, so I would stop trying to do that as soon as possible.
As you saw in the other thread, the behavior is inconsistent and relying on it means it may stop working in the future (sometimes without warning, so you may have trouble figuring out what’s wrong). This is why “hacky” solutions are not suitable for serious development projects.
I don’t think sounds play in storage parents. Try turning on PlayOnRemove, then parenting it to SoundService or the Workspace, and destroy the sound. That should be giving it a global playing effect.
Sounds don’t require a physical location to propagate from, but they only play from certain locations. For the sake of getting a proper door opening sound, you should be putting the sound in the door somewhere under a part or attachment. What’s the aversion for doing so?
Sounds don’t work in storage services the same way anything else with run time operability doesn’t. Storage services are meant only for storage. It does not make sense for a sound to be in a storage service unless it’s for reference or you plan to take it out of storage at some point. You can also put sounds in SoundService for global playback.
Don’t force the mindset, work around it and change yours. Sounds should be placed in an appropriate location for playback. Storage services should only be used to hold objects that you want to pull out later. Exception to some items such as ValueObjects (hold pure data in Value) and ModuleScripts (run on require regardless of location).
AFAIK SoundService.PlayLocalSound can allow playback of a sound location regardless but you lose a significant amount of functionality in exchange for using it and it’s client-side only.
Please don’t do this. You don’t have any actual reason to destroy the sound, especially if you’re going to use it often. PlayOnRemove honestly has little to no legitimate use case except for playing the sound as a parent object gets removed, since you can’t play the sound later.