Playing multiple audios at the same time

Hey there. As a relatively inexperienced and new scripter, I am still trying to get my way around Lua.
Anywho, the main objective of what I am trying to do is to get a sound instance from ReplicatedStorage to some parts in a folder, then for all of them to play at the same time. What I mean by the same time is that they all start at the same time, not having a few ms delay between them.
I have already solved the replication part, its just the playing bit I’m having trouble with.

See, I want to primarily use a ServerEvent for this, though once I have finished writing this and some people comment, you can disagree with that.

I want to know what the best way to do this is, because I have looked all over youtube and the dev forum and no one has really asked this question.
Thanks!
If you want to see the code I used, it is here.

EventReciever

Event = game.ReplicatedStorage.SoundEvent
Event.OnServerEvent(function()
script.Parent.Sound:Play()
end

SoundReplicator

Sound = game.ReplicatedStorage.Sound
File = game.Workspace.TestFolder:GetChildren()
Event = game.ReplicatedStorage.SoundEvent
local function AddMusic()
for Number, Part in pairs(File) do
local ReplicatedItem = Sound:Clone()
ReplicatedItem.Parent = Part
end
end
wait(2)
print(“Got here”)
AddMusic()
Event:FireServer()

Also if any of my scripting is broken, which it is as the event fire doesn’t work, please help me.
Any help you could give would be awesome. Thank you.

1 Like

Not sure why they are not playing at the same time probably because the listeners for the remote event aren’t keeping up and each one is listening slightly late comparing to another one.
So I guess what you could do is make all the sounds play from one script, so probably loop through them and play all of them.

for i, v in pairs(file) do
v:Play() --you might as well maybe use spawn(), check this function out
end
1 Like

Are these pieces of code on Scripts or Local Scripts? FireServer() should be called from a Local Script, and OnServerEvent should be used in a Script.

The SoundReplicator is a local script, and the EventReciever’s are normal scripts.

A problem that you may be having is that you need to connect function() to OnServerEvent, this can be done very easily:

Event = game.ReplicatedStorage.SoundEvent
Event.OnServerEvent:Connect(function()
script.Parent.Sound:Play()
end
1 Like