Simple Module Problem

I got a moduleScript which has 2 funtions and 1 loop. I want to activate the loop when the number 1 function called and stop the loop when the 2nd function is called.

local timeHandler = {}
local DoStart = false

timeHandler.StartTime = function()
	DoStart = true
end
timeHandler.StopTime = function()
	DoStart = false
end
while DoStart == true do
		print("YES")
		wait(1)
	end
return timeHandler

I tried but my brain can’t figure out a way.

Have you tried putting the while loop inside of the StartTime function?

yep, it didn’t work when the stop was called. kinda weird it yeilds the server script when the loop starts

local timeHandler = {}
local DoStart = false

timeHandler.StartTime = function()
	DoStart = true
end

timeHandler.StopTime = function()
	DoStart = false
end

coroutine.wrap(function()
	while true do game:GetService("RunService").Heartbeat:Wait()
		if DoStart == true then
			print("YES")
			wait(1)
		end
	end
end)()

return timeHandler
1 Like

it doesnt even get called.

local WorldClock = require(script.TimerMod)

WorldClock.StartTime()
wait(2)
WorldClock.StopTime()


I edited it, try it again
tell me if it doesn’t work

local timeHandler = {}
local DoStart = false

timeHandler.StartTime = function()
	DoStart = true 
    coroutine.wrap(function()
        while DoStart == true do
	    	print("YES")
    		wait(1)
    	end
    end)()
end
timeHandler.StopTime = function()
	DoStart = false
end
return timeHandler

If you want to avoid yielding the script, then i’d suggest a coroutine.

1 Like

Here’s what I would do:

local timer = {}
timer.__index = timer

function timer.new()
	local self = setmetatable({},timer)

	self.currentTime = 0
	self.enabled = false
	return self
end

function timer:Start()
	self.enabled = true
	coroutine.wrap(function()
		while self.enabled do
			self.currentTime += 1
			wait(1)
		end
	end)()
end

function timer:Pause()
	self.enabled = false
end

function timer:Stop()
	self.enabled = false
	setmetatable(self,nil)
end

return timer

In another script that requires a timer, you can initialize the module by requiring it.

local timerMod = require(game.ServerScriptService.timerMod)

To create a new timer object, you can use timerMod.new() and assign it to a variable (like so:)

local timer = timerMod.new()

When the timer has been created you can call :Start() on it to begin the timer.

timer:Start()

You can also print the properties of the timer:

print(timer.currentTime) --> Prints the current time of the timer
print(timer.enabled) --> Prints if the timer is currently running

To pause the timer at any given time, you can call :Pause() on the timer object. The timer will stop counting and will only start again when :Start() is called again.

And the final function, :Stop() will destroy the timer.

I dont expect it to work absolutely perfectly but it does seem to fit your case. Feel free to use the module and/or improve it.

2 Likes

this is much better then my solution, it also uses OOP which really good. thanks for calling me out on that

1 Like

Actually, I am making a school system just like RoyaleHigh one but instead of a timer I need a 12h day/night cycle. Thanks for this tho, I will improve on it and OOP does look quite clean.

1 Like