Is a coroutine needed here? If so, how would I use it the most effective way?

Hey! I have never used coroutine before. Basically, when the player collects it it disappears for 40 seconds. Since there could be multiple being used, and it is a ModuleScript, I believe that a coroutine would be needed here. What would the best way to use it be?
Thanks for the help!

Code

local R_Functions = {}

function R_Functions.Function1_ScrapCollect(scrap, player, points)
	scrap.Collectable.Value = false
	scrap.CanCollide = false
	scrap.Transparency = 1
	player.leaderstats.Points += script.Parent.PointsValue.Value
	player.PlayerGui.PointsGui.PointsGained.Text = "Gained "..script.Parent.PointsValue.Value.." Points!"
	player.PlayerGui.PointsGui.PointsGained.Visible = true
	task.wait(2)
	player.PlayerGui.PointsGui.PointsGained.Text = "Gained (Points)"
	player.PlayerGui.PointsGui.PointsGained.Visible = false
	task.wait(38)
	scrap.Collectable.Value = true
	scrap.CanCollide = true
	scrap.Transparency = 0
end

return R_Functions

After testing, it appears no. I may be wrong, but it seems like I don’t need coroutine.

That depends how it’s being called.

If you’re just connecting this directly to an event, for instance, it will be run in a new coroutine automatically:

....Touched:Connect(function() mymodule.Function1_ScrapCollect(...) end)
1 Like

Thanks. It appears to do that. I am running .Touched so it appears I don’t need to. Thanks!