Sounds randomly don't play in function

Hi I’m making a game that relies on a sound effects system, I’m doing it in a module script but sometimes it will randomly not play the audio and still print out the sound id. I think it might be a thing of not loading in time but I dont know how to make it wait until the audio fully loads up as it seems like a random amount of time. Any help would be appreciated!

function speechModule.playSound(soundEffect,volume,length)
	local sound = Instance.new("Sound", workspace.SoundEffects)
	local soundID = tonumber(soundEffect)
	if soundID then
		sound.SoundId = "rbxassetid://" .. soundEffect
	else
		sound.SoundId = soundEffects[soundEffect]
	end
	
	print(sound.SoundId)
	sound.Volume = volume
	if length == nil then
		sound:Play()
		wait(0)
		Debris:AddItem(sound,sound.TimeLength)
	else
		sound:Play()
		Debris:AddItem(sound,length)
	end
end

if the problem is the sounds randomly not playing, this could probably be for not being able to load before the event

this can be fixed with game:GetService("ContentProvider"), wich makes the instances preload ( like animations and sounds)

just do ContentProvider:PreloadAsync(Folder:GetChildren()) to load the instances of a folder

1 Like

I tested this quite a few times and it seems to have worked!! Thanks I had no idea something like that existed!

1 Like

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