How would I go about using OOP to create an appliance that would perform an action every few seconds?

Using OOP, I want to be able to create multiple machines that would perform a certain action every few seconds, however, the time that these objects would perform these actions would be different between each machine.

For example, I want a computer with a timer of 5 seconds printing “I’m a potato” every 5 seconds, whilst another computer which will be also printing the same message, however would do so every 10 seconds.

I initially thought on creating an array with the all of the instances and looping through them, but I’m unsure on how to go from there. Is there is possibly a way to have these different objects continue running continuously on different threads within a single script easily?

1 Like

just make a script for each of those machines that prints out what you want it.

After I posted this question, I managed to figure out the answer to my question. My solution uses coroutines within an object’s function to have the function continue on different threads.

function Appliance:PerformAction()
	coroutine.resume(coroutine.create(function()
		while true do
			wait(self.ActionTimer) -- self.ActionTimer varies depending on the frequency of an object.
			print("I'm a potato")
		end
	end))
end

I mean, this works, but I’m bumping this to ask, does it scale? I’m facing a similar issue, and wonder if this is truly the only solution