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!