How to loop a sound smoothly

I want to make a spray can play a spray sound when the player clicks left mouse button

the problem is that the sound loops badly

this is the code:

function SetLength(sound, length)
	sound.PlaybackSpeed = sound.TimeLength/length
end



sprayCan.Equipped:Connect(function()
	sound = script["Spray Paint 8 (SFX)"]
	spray = script["Aerosol Spray Can 3 (SFX)"]
	shake = script.Parent.Shake
	shakeAnim = Character.Humanoid:LoadAnimation(shake)
	
	hold = script.Parent.Hold
	holdAnim = Character.Humanoid:LoadAnimation(hold)
	
	shakeAnim:Play()
	sound.PlaybackSpeed = 1.5
	sound:Play()
	shakeAnim.Ended:Wait()
	--the sound must be loaded, therefore it only works on the client

	holdAnim:Play()
	equipped = true


end)

sprayCan.Unequipped:Connect(function()
	equipped = false
	holdAnim:Stop()
	spray:Stop()
end)

while wait() do
	local pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
	if pressed then
		if equipped then
			if not spray.Playing then
				load = spray.IsLoaded or spray.Loaded:Wait()
				SetLength(spray, .5) --34 is the new length(in seconds)
				spray:Play()
			end

			calculateSprayInfo()
		end
	else
		if spray ~= nil then
			if spray.Playing == true then
				spray:Stop()
			end 
		end
	end
end

there aren’t any problems in the code, but maybe there’s a small section of the audio that contains nothing? I think you should check

Yeah I think there is but I thought if I could shorten it with code it would work

Usually what they doing is making longer the part they want in another audio application. Try to find the right sound or try to change TimePosition to spraying when the audio has finished and player still holding the button.

Ive fixed it now by using this Playing a quickly repeating sound efficiently without the annoying 'click' sound

1 Like

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