Hi , So i’m making a custom time module (I know i’m reinventing the wheel but im doing for fun ) So the problem i have is that because my Timer:Start() function uses a while loop to count it never finishes so the main script will never progress pass the the timer:Start() line
Thanks.
local Timer = {}
Timer.__index = Timer
function Timer.new(StartTime)
local NewTimer = {}
setmetatable(NewTimer,Timer)
NewTimer.Running = false
NewTimer.Time = StartTime
return NewTimer
end
function Timer:Start()
self.Running = true
while self.Running == true do
wait(1)
self.Time = self.Time - 1
end
end
function Timer:Stop()
self.Running = false
end
function Timer:Value()
return self.Value
end
-- Main Script
TimerModule = require(game:GetService("ServerStorage").TimerModule)
local timer = TimerModule.new(200)
timer:Start()
wait(2)
print("A") -- Never Does these lines?
print(timer:Value())
So i cant use the :Stop Function under because it will never run these lines, I don’t necessarily want to fix my module i want another way of dealing with time if possible? Thanks
You should try running Timer:Start() in a coroutine. That way, you can set self.Running to whatever value you want and it wont yield the thread.
If I were making a custom timer module, I would stay away from while loops in general and opt for a heartbeat-based wait instead. More precision and easier to stop imo.