Wait yielding forever?

Hey! I’m having a problem with a coin collection module that I’m making. The issue is that when the player collects a coin, coin particles are supposed to pop out and the coin disappears. Currently, all of it works except for the particles, when you touch the coin the particles appear and then never go away.

I believe I have narrowed down the problem to a line of code, so take a look:

Line calling module (This function is called when a coin is touched) -

function coinSpawner.collectCurrency(part, cType, amount, player) -- The part parameter here is the coin that was touched.
	coroutine.wrap(particleModule.createEffect)(part.Attachment.Collect, part.Position, .1) -- Here is where the createEffect function is called.
	spawnedCurrency[part] = nil
	part:Destroy()
	if player.MembershipType == Enum.MembershipType.Premium then
		playerModule.awardPlayer(player, amount, cType, true)
	else
		playerModule.awardPlayer(player, amount, cType, false)
	end
end

createEffect function (The function in a module that creates an effect) -

function particleModule.createEffect(effect, position, duration)
	local part = Instance.new("Part")
	part.Size = Vector3.new(1, 1, 1)
	part.CFrame = CFrame.new(position)
	part.CanCollide = false
	part.Anchored = true
	part.Transparency = 1

	local newEffect = effect:Clone()
	newEffect.Parent = part
	newEffect.Enabled = true
	part.Parent = workspace
	
	print("waiting") -- Prints
	wait(duration) -- Where I believe the issue is, somehow.
	print("waited") -- Doesn't print
	newEffect.Enabled = false
	wait(newEffect.Lifetime.Max)

	part:Destroy()
end

I don’t believe this is a Luau bug or anything but it’s odd how I’m passing in the argument .1 for the duration and the wait is yielding indefinitely. This may just be a very stupid error on my part with modules and whatnot, but hopefully, someone can help me.

1 Like

I don’t know much about coroutines, but could you try printing out the duration variable in the createEffect function? It may return back as nil or something

You could also try this maybe?

coroutine.wrap(particleModule.createEffect(part.Attachment.Collect, part.Position, .1))

I printed the duration and it printed .1, as expected.

I tried what you said and it threw an error, but when I took the function out of the coroutine it worked but caused a massive delay from when the coin was touched and when it was actually awarded.

Ive just tested this in an empty script and removed everything apart from the duration, and it seems to work fine, maybe the issue is somewhere else?

It could be a problem with the coroutine, when I call the function without the coroutine it seems to work fine but causes a delay from when the coin is touched and rewarded.

I’m going to play around with the coroutines for a bit and see if I can fix this.