Using "RunService" vs. "Repeat Wait() Until" for Tycoon Droppers?

I’m making a dropper system for a new tycoon of my Studio’s coming out soon.

The droppers would require no input from the user(s), as the droppers would just constantly be dropping bricks for the user’s income in a loop - and I am wondering what type of loop I should use.

The way the dropper system will work, is when the player buys a dropper, an OOP object will be created, and this OOP object will have a loop run inside of it, until the physical connection (a variable that will be set to the dropper model) is nil/no longer parented somewhere in Workspace, meaning that the user/owner of the tycoon left the game or rebirthed.

In the test place I was using, I was originally planning on using the following:

---everything is defined

function dropper:Destroy()
	local janitor = self._janitor
	if janitor then
		janitor:Destroy()
		self._janitor = nil
	end
end

function dropper.new(dropperModel)
	local self = setmetatable({
		_janitor = Janitor.new(),
		dropModel = dropperModel,
	}, dropper)
	self._janitor:Add(RunService.Heartbeat:Connect(function(step)
			--- create the dropped brick here
			task.wait(1)
		end), "Disconnect")
	self._janitor:Add(game.Workspace.ChildRemoved:Connect(function(removedItem)
		if removedItem == dropModel then
			self:Destroy()
		end
	end), "Disconnect")	
end

That would be using RunService and the Janitor module

But I was wondering, would this be more efficient?

function dropper.new(dropperModel)
	local self = setmetatable({
		_janitor = Janitor.new(), --- would i even need to use it in this case?
		dropModel = dropperModel,
	}, dropper)
	repeat
		--- created dropped brick here
		task.wait(1)
	until dropModel.Parent == nil	
end

I would appreciate if someone could please give me an answer on what would be the most efficient and would have the least memory leaks. Thanks!

1 Like

Using the second method would be more effective since you would be only running the loop once per second instead of every frame.

Is there a way I could limit RunService to running once per second? In addition, I know RunService is pretty memory-heavy and a janitor for using it is usually recommended, I wouldn’t need to do that for repeat until, right?

Runservice fires an event every frame.

I don’t know much about what janitor does, but I don’t think you need to do anything special for a repeat loop.

I’m not sure why you would use a Connection to Fire for Every frame. That Doesnt seem every efficient, The Second version would probably be better for performance.

I don’t know how to explain it in detail/very well, but essentially janitor just prevents memory leaks by making sure connections to objects/functions that could lead to memory leaks are cleaned up when appropriate.

One other thing I was considering, was a script that would create a new dropper object, insert that object into a table, and then have a function that, every second using either repeat wait until or RunService, would drop a block.

When a player leaves or whatever, their dropper(s) would be removed from that table.