basically when im trying to pre load the sound, it wont play
local player = game.Players.LocalPlayer
local sound = game.ReplicatedStorage.sounds.StandSound
game:GetService("ContentProvider"):PreloadAsync({sound.SoundId})
sound.Loaded:Connect(function()
print("yes")
for i, v in pairs(workspace.Builds.spawn.stands:GetChildren()) do
v.ClickDetector.MouseClick:Connect(function(plr)
if player == plr then
local clonedsound = sound:Clone()
clonedsound.Parent = v
clonedsound.Playing = true
game:GetService("Debris"):AddItem(clonedsound, 1.118)
end
end)
end
end)
thanks
Edit: im not sure u will need this info but the location of local script is StarterPlayerScripts
Looks like you’re having a race condition between PreloadAsync and Loaded. PreloadAsync does not determine if the sound is loaded or not, it only advances the ContentIds associated with the passed descendants to the front of the download queue.
Besides that, your issue is non-specific. “It won’t play” isn’t enough information. You haven’t mentioned anything about if the print goes through or not, if the click function is being called or if everything besides the sound playing works. Also, curious about your choice of setting Playing to true rather than just calling the Play method, though that’s secondary to the aforementioned.
PreloadAsync yields until all the assets within the array has been loaded. However, because you connect the sound.Loaded function after the PreloadAsync, sometimes it gets connected after it has already loaded, and thus it will not fire again.
That still doesn’t explain much. You need to include actual detail. Try debugging and pointing towards where the actual issue is. The bottom half of my post goes into some depth about unanswered points that your thread has. Read: Debugging
Imagine a line of assets waiting to be downloaded: A, B, C, D, E, F and G. You call PreloadAsync on asset G. The line is now G, A, B, C, D, E, F. Assets are downloaded in a queue. That download queue has no affect on whether or not a Sound’s Loaded event fires.
Your code is heavily reliant on timing and that can subject it to race conditions. Your code should be written more defensively. Particularly, in this case, there’s no reason to use Loaded at all.
As colbert explained, PreloadAsync will just move the asset to the first place in the loading queue, and when all assets inside of PreloadAsync have loaded, it will continue running the code below it. There is no need to use the Loaded event, because PreloadAsync yields for the specified assets to load.