Sound Delay Latency (Local)

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    wanted to create a piano player

  2. What is the issue? Include screenshots / videos if possible!
    as you can see when I play a key, there is a noticeable delay when playing the key sound. And after that it doesn’t delay anymore until you wait long enough .

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    already searched the dev forum but found nothing

video:

code:

function PlayNoteSound(note, source, range, sounds, vol, SpecialPiano)	
	local pitch = 1
	if note > 61 then
		pitch = 1.059463 ^ (note - 61)
	elseif note < 1 then
		pitch = 1.059463 ^ (-(1 - note))
	end
	note = note > 61 and 61 or note < 1 and 1 or note
	local note2 = (note - 1)%12 + 1	-- Which note? (1-12)
	local octave = math.ceil(note/12) -- Which octave?
	local offset = (16 * (octave - 1) + 8 * (1 - note2%2))
	local sound = math.ceil(note2/2) -- Which audio?
	
	local audio = Instance.new("Sound", SoundFolder) -- Create the audio
	audio.SoundId = "https://roblox.com/asset/?id="..((sounds and sounds[sound]) or LocalSounds[sound]) -- Give its sound
	
	if source then
		local a = 1/range^2
		local distance = (game.Workspace.CurrentCamera.CoordinateFrame.p - source).magnitude
		local volume = -a*distance^2 + 1
		if volume < 0.01 then
			audio:remove()
			return
		end
		audio.Volume = volume * vol
	else
		audio.Volume = Volume;
	end
	if fade then
		for i = 1, 5 do
			audio.Volume = audio.Volume * 0.75
			wait(0.1)
		end
		audio:Stop()
		audio:Destroy()
	end
	
	local octave = offset + ((SpecialPiano or specialPiano) and 0.04 or (octave - .9)/15)
	
	game.ReplicatedStorage.MusicEvents.Piano:Fire(audio.SoundId, octave, pitch)

	audio.TimePosition = octave -- set the time position
	audio.Pitch = pitch
	audio:Play() -- Play the audio
	
	table.insert(ExistingSounds, 1, audio)
	
	if #ExistingSounds >= 15 then
		ExistingSounds[15]:Stop() -- limit the number of playing sounds!
		ExistingSounds[15] = nil
	end
	delay(5, function() audio:Stop() audio:remove() end ) -- remove the audio in 4 seconds, enough time for it to play
end

So how do i fix the delay? any helps would be appreciated

You could try making the sound files when the game starts instead of waiting for it to load when each key is pressed.
It could also be because this is server sided and the client experiences a delay.

1 Like

Ok, will try! (30caharharhashra)

You want to preload the sound assets if you want to avoid the delay as the client otherwise has to download the sounds first when they are played for the first time:

2 Likes