Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How am I supposed to Preload Sounds in a script?

Asked by 6 years ago

I need help my sound in my script wont play so I want to preload it so it can play. I was wondering if you know any advice you could give me on this topic?

0
paste the script so we know what you mean wookey12 174 — 6y

2 answers

Log in to vote
1
Answered by
mraznboy1 194
6 years ago

When you preload a sound, you have to use the ContentProvider service from Roblox in a LocalScript.

local ContentProvider = game:GetService("ContentProvider")

Then, you use the ContentProvider's function PreloadAsync to preload all assets that are in a table that you pass into the function like so:

local ContentProvider = game:GetService("ContentProvider")
local PreloadItems = {
    Sound1,
    Sound2,
    etc
}
ContentProvider:PreloadAsync(PreloadItems)

The way that the PreloadAsync works is that the table of items must be Objects in the workspace. For instance, when you want to preload a sound, you need a Sound Object in the workspace or in a folder in the workspace. What I like to do is have a Folder named "PreloadObjects" in the workspace filled with the sounds that I need to preload. Then you can access all of them into a table as a :GetChildren() table like so:

local ContentProvider = game:GetService("ContentProvider")
local PreloadItems = workspace.PreloadItems:GetChildren()
ContentProvider:PreloadAsync(PreloadItems)

If you want to see a visual of how your queue of preloaded assets are going, you can view them with ContentProvider.RequestQueueSize.

local ContentProvider = game:GetService("ContentProvider")
local PreloadItems = workspace.PreloadItems:GetChildren()
ContentProvider:PreloadAsync(PreloadItems)
while ContentProvider.RequestQueueSize > 0 do wait() end 
print("Done preload!")
Ad
Log in to vote
-1
Answered by 6 years ago

My guess is that you should use WaitForChild.

local sound = script:WaitForChild("Sound")

Answer this question