How can I coroutine.wrap() this function?

Trying to make a function to transition from one sound effect to the next, but in order to not yield the script, I’d like to wrap it in a coroutine. I don’t know how to both do this and also send in sound1 and sound2, though.

local function soundTransition(sound1, sound2)
	local s1v0 = sound1.Volume -- Sound 1 initial volume
	local s2v0 = sound2.Volume -- Sound 2 initial volume
	
	sound2.Volume = 0
	sound2.Playing = true
	
	tweenService:Create(sound1, soundInfo, {Volume = 0}):Play()
	tweenService:Create(sound2, soundInfo, {Volume = s2v0}):Play()
	
	wait(0.3)
	
	sound1.Playing = false
	
	sound1.Volume = s1v0
	sound2.Volume = s2v0
end

coroutine.wrap(soundTransition) -- this way doesn't allow me to send in sound1 and sound2...
2 Likes

You have to actually call the coroutine as well, when you do this you can pass arguments:

coroutine.wrap(soundTransition)(sound1, sound2)
6 Likes