Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Timer module min & sec not working? 0:00

Hello! Again another problem. Lets just dive in

I have a function in a timer modulescript called timer:Play() Here's the problem.

It works fine if it was no minutes and just seconds but problem is that when you put min and second once the seconds reach 0 then for a example like 2:05 after 5 seconds it just goes 1:59 and just freezes there I tried making the sec variable into i but the same result. Here is the code if you didn't know self is a variable created by a modulescript when you set a metatable onto the module table inside the table is

--sec and min is a parameter in timer.new
	local table = {
		started = false;
		finished = false;
		stopsignal = false;
		min = minutes;
		sec = seconds
	}

Anyways lets get onto the code

	self.started = true
	local min = self.min
	local sec = self.sec
	for i = sec,0,-1 do
		sec = sec - 1
		for _,player in pairs(game.Players:GetPlayers()) do
			player.PlayerGui.Timer.Enabled = true
			if sec > 10 then
				player.PlayerGui.Timer.Time.Text = min .. ":" .. sec
			else
				player.PlayerGui.Timer.Time.Text = min .. ":0" .. sec
			end
			if (sec <= 0 and min >= 0) then
				min = min - 1
				sec = sec + 60 --i think something is wrong here
			end
			if min < 0 then
				self.finished = true
				player.PlayerGui.Timer.Time.Text = 0 .. ":" .. "00"
				print("Done")
				break
			end
		end

Sadly no errors so i couldn't solve this myself Here is the script code

local thing = require(game.ReplicatedStorage:WaitForChild("Timermodule"))
local stuff = thing.new(0,59) 
script.Parent.MouseClick:Connect(function()
stuff:Play()
end)

1 answer

Sorry i can't really solve your case but i can teach you better method of making timers. I see that your question is almost day old and still has no answers so i can show you better method than yours instead. The better method i mean is formatting strings. If you don't exactly know what it is, it's just string patterns in which you format your own strings, there are different formatting patterns which are in the link. For example pattern %s stands for formatting strings so you would use it like this:

string.format('I like %s', 'cats')
--/ Since %s stands for strings format, you would need to put second
--\ argument as a string, this will format the string and return it so in
--\ this case the result will look like this: [I like cats]. That's because
--\ it formatted word cats into the %s pattern

So there is a pattern %d which stands for digits (Numbers), on the site you can also read about 0 (zeros) in patterns which are used before the %d pattern and are very usefull for timers like this, here is example of %d pattern in use:

string.format('%d + %d = %d', 10, 20, 30)
--/ This will format all next arguments into the string so the result
--\ should look like this: [10 + 20 = 30] (as string)

Now the zeros i talked about add given amount of 0 to the %d so you can use this to make the first step of a timer:

--|||||| NOTE ||||||--
--/ So apparently it removes all the patterns that look like a phone
--\ number or what so they are written in spaces but make sure  you
--\ remove them because they are probably going to break the patterns

string.format("%0 2d", 1) -
--/ This will return [01]

string.format("%0 3d", 10)
--/ This will return [010]

string.format("%0 1 2d", 1) 
--/ This will return 000000000001

So for the seconds in the timer you can do:

local Seconds = string.format('%0 2d', sec)
--/ Which if [sec] is for example 60 will return [60]

And same for minutes. So to convert it all into normal timer, i here made simple function for that called ToTimer, here is how it looks:

--/ It takes Seconds and Minutes and returns them formatted as a timer
local function ToTimer(Seconds, Minutes)
	return string.format('% d:%0 2d', Minutes, Seconds)
end

So your whole script can look like this:

--/ You can put this function somewhere if you need it
--/ It's just one line so if you don't use it much, feel free
--/ to just remove it
local function ToTimer(Seconds, Minutes)
	return string.format('% d:%0 2d', Minutes, Seconds)
end

self.started = true
local min = self.min
local sec = self.sec
for i = sec,0,-1 do
    	sec -= 1
    	for _,player in pairs(game.Players:GetPlayers()) do
        	player.PlayerGui.Timer.Enabled = true

		--/ Here you see it saved you like 10 lines
		player.PlayerGui.Timer.Time.Text = ToTimer(sec, min)
    	end
end
--/ Now here you can put it outside the loop because when [sec]
--\ variable hits 0, the timer breaks so the code below will run
--\ right after that, you can move it back if you need it for your case
	
self.finished = true
print('Done')

Also note i used -= on line 12 instead of sec = sec - 1, it does completetly same and you can do it with any symbol:

local Number = 50

--/Those two are same
Number = Number - 50
Number -= 50
--/Those two are also the same
Number = Number * 50
Number *= 50
--/And those are same too
Number = Number + 50
Number += 50

I reccomend you using that because it is shorter and faster to type but your choice.

Also i did not test this so if there are any errors i don't know but you are no the biggest scripting noob i see so you can make it work.

Okay so i think i get the reason, it's with the seconds because you loop through the seconds but there are also minutes so instead, you can multiply minutes and count them into the seconds to get precise loop, by that i mean:

local min = self.min
local sec = self.sec

--/ Variable that for loop will loop through, it is
--\ seconds multiplied with minutes, minutes are
--\ multiplied by 60 here because one minute is 
--\ 60 seconds if i am sure
local Time = sec + (min * 60)

for i = Time,0,-1 do

Of course what would be easier is not to use minutes and seconds but just one number value but you do not have to with this method. If even this does not work then i am stupid, sorry.

2 comments

0

Yeah it did have some errors so i fixed it and got it working! It still does do the pause at 59 thing though

0

I posted small edit, maybe that can help fix it? If not i am just going to post method that i am sure will work but will have bit different loop although i do not like to change the scripting tactics of someone but i guess i maybe should.

This discussion is preserved as it was. New answers, comments, and votes are closed.