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

How to Pre-load an sound asset?

Asked by 8 years ago

This code works with images but not with sounds.

local ContentProvider = Game:GetService("ContentProvider")

local function LoadAssets(AssetList)
    -- Takes an asset list and preloads it. Will not wait for them to load. 

    for _, AssetId in pairs(AssetList) do
        ContentProvider:Preload("http://www.roblox.com/asset/?id=" .. AssetId)
    end
end

LoadAssets({2253543, 2434541, 5133543, 2423433, 41143243, 2453865, 21433365, 2154549})

I don't know why it doesn't work.

1 answer

Log in to vote
0
Answered by 8 years ago

Pre-loading assets works with all assets not just sound. It needs to be done on the client side as they are downloading these assets into their cache meaning that when you use the asset in game there will not be any delay as the client already has the content. You can tell if a game does not pre-load assets when there is a delay for decay loading or sound playing.

This is part of my load screen script, this script needs to be a localscript in replicated first.

local contentProvider = game:GetService("ContentProvider")
local assets = {

    -- load screen assets
    "347778466", -- sword logo left
    "408497296", -- sword logo right

    -- i dont think songs need assets removed?? 
    "305161877", -- some random song i like
    "165065112"
}

-- loads the asset into game
for _, asset in ipairs(assets) do
    contentProvider:Preload("http://www.roblox.com/asset/?id=" .. asset)
end

You can also track the loading by using "RequestQueueSize" this is a sample from my code:-

local assetNum = contentProvider.RequestQueueSize
local curPer = 0
local curReqSize = 0

-- load bar and txt checking
while contentProvider.RequestQueueSize > 0  do
    curReqSize = contentProvider.RequestQueueSize -- the current request size

    if curReqSize < assetNum then -- check the request is smaller than the last
        local per = math.abs(math.floor(curReqSize / assetNum * 100) - 100)

        if per > curPer then -- check that the percentage is also smaller
            print(per)
            curPer = per
        end

    end
    wait(0.8)
end

Hope this helps.

Ad

Answer this question