I'm making a GUI where when you type text into a textbox then press "Play" it will play that audio in a brick but it doesn't seem to be working. Im getting the error:
Failed to load sound rbxassetid://blarfart: Unable to download sound data (x2)
and
Here is the script inside of the button:
1 | script.Parent.MouseButton 1 Click:Connect( function () |
2 | local SoundID = script.Parent.Parent.Music.Text |
3 | game.ReplicatedStorage.Music:FireServer(SoundID) |
4 | end ) |
And in the brick
1 | game.ReplicatedStorage.Music.OnServerEvent:Connect( function (SoundID) |
2 | script.Parent.Sound.SoundId = "rbxassetid://" ..SoundID |
3 | script.Parent.Sound:Play() |
4 | end ) |
You might want to wait the script until the sound is actually loaded from Roblox's Servers, that's probably the issue you're facing. You can use Sound.IsLoaded
for this. The example used by Roblox is below, you can read more about it here.
1 | local function loadSound(sound) |
2 | -- Has the sound already loaded? |
3 | if not sound.IsLoaded then |
4 | -- if not, wait until it has been |
5 | sound.Loaded:Wait() |
6 | end |
7 | end |
8 | loadSound(Workspace.Sound) |
In your instance, I'd implement it like what I have below.
1 | game.ReplicatedStorage.Music.OnServerEvent:Connect( function (SoundID) |
2 | script.Parent.Sound.SoundId = "rbxassetid://" ..SoundID |
3 | if script.Parent.Sound.IsLoaded then |
4 | script.Parent.Sound:Play() |
5 | end |
6 | end ) |
Hope that helps, consider accepting this answer if it helps at all. If not, comment and I'll be sure to assist you further.
This is probably because you're not allowing the audio to load first.
You should try this:
1 | game.ReplicatedStorage.Music.OnServerEvent:Connect( function (SoundID) |
2 | script.Parent.Sound.SoundId = string.format( "rbxassetid://%i+" , SoundID) |
3 | if not script.Parent.Sound:IsLoaded() then |
4 | script.Parent.Sound.Loaded:Wait() |
5 | end -- this will yield until loaded |
6 | script.Parent.Sound:Play() -- now play |
7 | end ) |
For more info on this, check out this article