I can't preload my sound

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

2 Likes

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.

1 Like

It doesnt, I added it to test and the problem is in Loaded event

1 Like

Can you please re explain this part? I didnt really understand it

1 Like

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.

1 Like

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.

1 Like

Okay, I realized now, thanks, but how do i detect if it downloaded?

1 Like

PreloadAsync will yield until the assets have all been downloaded. All the code that comes after will have the ID already downloaded.

1 Like

I dont understand, so does preloadasync connect to loaded event? or loaded and downloaded are 2 different stuff?

1 Like

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.

1 Like

Ohh, i realized now, thank you!

Colbert and green guy thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.