Is there a better way to avoid theses multiple simultaneous task.wait()?

Hello, I’m trying to create a Obby mechanic → a Part becomes transparent and then becomes apparent (when the part is invisible then the part becomes non collidable and vice versa).

Indeed, the script below is functional for the game, but I’m wondering if it’s really optimized, in case there are a hundred parts, then it would create a lot of simultaneous task.wait(2) (when the part becomes invisible there’s a two-second delay before the mechanic resumes), and I’m wondering if this will cause lag for the client.

Do you have another way of doing this mechanic in mind? Or on the contrary, do you think that having many simultaneous task.wait(2) is not a problem?

Thank you very much for your advice.
Note: This loop handles the whole game, but for the example I’ve shortened it just for the transparent parts.

TimeConnection = runS.RenderStepped:Connect(function(dt)
	for i,v in workspace.Tour.ScriptedPart.TransPart:GetChildren() do
		local config = v.ConfigTransp
		if config.Start.Value == true then
			v.Transparency += config.Speed1.Value
			if v.Transparency > 0.99 then
				config.Start.Value = false
				v.CanCollide = false
				coroutine.wrap(function()
					task.wait(2)
					v.CanCollide = true
					config.End.Value = true
				end)()
			end
		elseif config.End.Value == true then
			v.Transparency -= config.Speed0.Value
			if v.Transparency < 0.01 then
				config.End.Value = false
				config.Start.Value = true
			end
		end
	end
end)

sefsefggfdg

task.wait(2) pretty much just tells Roblox it can stop running that code for 2 seconds. No significant work is being done during that time so task.wait() shouldn’t really impact your game in terms of performance.

1 Like