Is there a way to detect each time a minute has past live?

% is the modulus operator. x % n is the remainder if you were to do integer division of x by n

5 % 3 is 2 because 5 / 3 = 1 + 2/3; 3 goes into 5 once, and there is a left over 2. So there is a remainder of 2

2 Likes

Why does it need to be exact? Can’t you just, you know, have it a few milliseconds off? It seems very performance heavy for something that doesn’t really do much.

local minutes = 0

while wait() do
	local time = os.time()
	local minutesSinceEpoch = time / 60

	if math.floor(time) / 60 == math.floor(minutesSinceEpoch) then
		minutes = minutes + 1
	end
end

You should note that will increment minutes 60 times every minute because it is firing roughly 60 times a second. There is 1 second time window where math.floor(minutesSinceEpoch) == minutesSinceEpoch is true; this comparison will be done every frame which is roughly 60 times. (60 fps)

image

Here is the demo code that illustrates the problem.

local minutes = 0
local RunService = game:GetService("RunService")
while RunService.Stepped:Wait()  do
	local minutesSinceEpoch = os.time() / 60
	if math.floor(minutesSinceEpoch) == minutesSinceEpoch then
		minutes = minutes + 1
		print(minutes)
	end
end

Easy way to fix this is minutes = minutes + 1; wait(1). You don’t want to do minutes / 60 because if you’re reading minutes at lets say the 19th frame it will be 19 and would not return an integer value if you were to divide by 60. There is also the problem that you might be at 59fps or suddenly drop to 30fps and dividing by 60 will yield half a minute or almost a minute but not really a minute.

(Also you forgot to initialize minutes as 0) You fixed this.

What about 1? Why is it there?

Okay, so it gives the remainder of a division, thank you for the help!