JetUpNy
|
  |
| Joined: 18 Feb 2016 |
| Total Posts: 258 |
|
|
| 17 Oct 2016 05:52 AM |
How would I format my time to 00:00:00 , I read about tick() and osTime and string formatting and I searched the forum but have no clue how to format it.
How would I stop this timer when the stop brick is touched and store the end value? I'm not sure whether to put a break in there or what. I've been learning and have progressed some what but I'm kinda stumped right here at the moment.
local mazeTime = 0 -- MazeTime Start at zero mzTimeStatus.Value = " timer:: "..mazeTime -- Set timer label to zero --Main Script Counts Down
debounce = false
function strtMzTimer(mazeTimer) if debounce == true then return end debounce = true
print("Maze Starter Touched") while wait() do for i = mazeTime,math.huge do wait(1) mzTimeStatus.Value = " timer:: "..i end -- end for statement
end -- end if statement debounce = false end -- end function
strtBtn.Touched:connect(strtMzTimer)
|
|
|
| Report Abuse |
|
|
Cheater
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 5258 |
|
|
| 17 Oct 2016 06:22 AM |
So let me understand that correct first. You have a timer that counts up from 0 to... basically infinite? And you want a stop button, to make the timer stop. Do you also want a continue button or a reset button?
And whenever the timer is stopped, you want the amount of time progressed, being shown in a string in the format of "HH:MM:SS" right? |
|
|
| Report Abuse |
|
|
Pheserus
|
  |
| Joined: 07 Aug 2016 |
| Total Posts: 69 |
|
|
| 17 Oct 2016 06:29 AM |
Its simple to convert time to that format. Here is how you would get it.
local start = tick(); local currentTimeInSeconds = tick() - start;
For every 3600 you subtract you get an hour For every 60 you get a minute. The rest are seconds. |
|
|
| Report Abuse |
|
|
JetUpNy
|
  |
| Joined: 18 Feb 2016 |
| Total Posts: 258 |
|
|
| 17 Oct 2016 06:29 AM |
@ Cheater You are spot on with your understanding and since you mentioned it yes, continue and reset could benefit the players in the future.
|
|
|
| Report Abuse |
|
|
Pheserus
|
  |
| Joined: 07 Aug 2016 |
| Total Posts: 69 |
|
|
| 17 Oct 2016 06:38 AM |
I don't know how much this will help you, but this is what I use for handling time.
[varables] local startTime = 0 local pauseTime = 0 local paused = false
[start] startTime = tick();
[pause] pauseTime = tick() paused = true
[resume] startTime = startTime + (tick() - pauseTime);
[get time] if (paused == false) then return tick() - startTime; end
if (paused == true) then return pauseTime - startTime; return
|
|
|
| Report Abuse |
|
|
JetUpNy
|
  |
| Joined: 18 Feb 2016 |
| Total Posts: 258 |
|
|
| 17 Oct 2016 06:39 AM |
| @Pheserus Alright, I'm going to try and work that in with the text label, thanks for your reply. |
|
|
| Report Abuse |
|
|
Pheserus
|
  |
| Joined: 07 Aug 2016 |
| Total Posts: 69 |
|
|
| 17 Oct 2016 06:43 AM |
Sorry I noticed I made a couple mistakes, but it should be pretty obvious. Like not adding paused == false to the resume function. And making 2 returns at the end of the get time function.
But otherwise its fine. |
|
|
| Report Abuse |
|
|
Cheater
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 5258 |
|
|
| 17 Oct 2016 06:53 AM |
I see. Well there's a couple different ways to do this. You could either work object oriented. Meaning you wouldn't use any global variables and the buttons each do have their own scripts with their own purposes, accessing a variable that's stored within the model, containing all buttons.
So what I see you have is a Start button. You'd also need a Stop, Continue and Reset button.
Now that I usually don't like this idea, I started working with global variables instead. Eventhough I keep hearing this being a bad idea, I just don't listen to them. Sometimes it is a good idea so screw those people.
Now, you could also give the timer a "status" variable. For instance "running" or "stopped" or "reset" or whatsoever. You could also get rid of the Continue button and instead making the Start button act as a Continue button as well. That's all up to your preference.
But in the end what you want is something of the following. You want two variables, that can be accessed by all the buttons.
TimerStatus: As told, so the looping timer knows what it's up to. Time: Self-explanatory... in seconds though
Now you want the starting button to include the infinite loop, which you kinda already got but I rather see as being a bit ineffective...
So your Start (and I'd say now Continue button together) will have something like this:
___ function Start()
while TimerStatus == "running" do TimerStatus = "running" --Make sure "TimerStatus" is also a global value or a StringValue wait(1) Time = Time + 1 --Make sure "Time" is either a global variable or some IntValue or NumberValue in your explorer end
end
--Your .Touched event here ___
Afaik multithreading on a single script isn't possible so I'm not adding some sort of debounce to it. Multithreading would only cause the script to run more than once at the same time and then of course increasing the timer a bit too fast. :3
Now the reset button is rather simple. It really only resets the number in "Time" to 0 and if you want to also changes the TimerStatus to "stopped" or whatsoever in order to stop the timer on instantly increasing again.
The "Stop" button is probably the tricky one you're having issues with. Well first thing you wanna do is, you want to put the "TimerStatus" to "stopped" or something like that of course. And after that you simply just read out the "Time" value and start storing each number to a variable. So hours becomes an own variable, minutes, and seconds. Don't overwrite the "Time" value itself!
Getting hours is as the other one just said, dividing the total amound of seconds by 3600 and rounding down the number (to avoid the strange rest...). Store that value into a variable "hours" or so. If you want to get numbers like "01" in case it's lower than 10, just put another if statement, concatenating the value into a string (you need it as string in the end anyways). Now for getting the rest, you want something like "restvalue" as a variable and do restvalue = Time % 3600. This is the modulus and will give you the rest. Then you do the same thing with that number again, however you divide it by 60 to get the minutes. And then round it down again blabla, get the rest again and you got your remaining seconds.
Then make a string like timestopped = ..hours..":"..minutes..":"seconds.
And output that string wherever you like. Done. Hope you could follow me. :3 |
|
|
| Report Abuse |
|
|
Cheater
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 5258 |
|
|
| 17 Oct 2016 06:56 AM |
@Pheserus That is of course a somewhat different but for some people easier way to do it. :3 My option is just the way I used to always do it. ^^ Eventhough it probably looks a bit more newbie-like... |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2016 06:56 AM |
math.floor(tick()/60)%60
is the amount of minutes i did mod 60 so that it will never be over 60 then use that to get the hour
or
math.floor(tick()/3600)%24 |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2016 06:58 AM |
for the end result, use string.format to format the given numbers into 2 integer digits each
print(string.format("%.2d:%.2d:%.2d",2,45,9)) |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2016 07:04 AM |
Here is an example:
while wait(0.5) do local t = tick() local sec = math.floor(t)%60 local min = math.floor(t/60)%60 local hour = math.floor(t/3600) print(string.format("%.2d:%.2d:%.2d",hour,min,sec)) end |
|
|
| Report Abuse |
|
|
JetUpNy
|
  |
| Joined: 18 Feb 2016 |
| Total Posts: 258 |
|
|
| 17 Oct 2016 07:28 AM |
| I'm truly thankful for all the knowledge and I'm going to try everything. A true test of my understanding will be a finished script. |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2016 07:32 AM |
i made the hours over 24
to fix that you can always do
%24 |
|
|
| Report Abuse |
|
|
Cheater
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 5258 |
|
|
| 17 Oct 2016 07:37 AM |
| Especially take a look at Wunder_Wulfe's messages. Because my stuff is pretty much only the basics of the basics. He's got some advanced stuff there that's sometimes cool to use too. |
|
|
| Report Abuse |
|
|
JetUpNy
|
  |
| Joined: 18 Feb 2016 |
| Total Posts: 258 |
|
|
| 17 Oct 2016 07:51 AM |
| @Cheater I understood your explanations and examples, I think but I will have to study your suggestions further |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2016 07:54 AM |
here is wat mod can do for you:
local t = 20 print(t%60) t = 60 print(t%60) t = 130 print(t%60)
it keeps you from having to subtract every time also is just the remainder in division |
|
|
| Report Abuse |
|
|
JetUpNy
|
  |
| Joined: 18 Feb 2016 |
| Total Posts: 258 |
|
|
| 17 Oct 2016 08:23 AM |
Wiki - Global namespace/String manipulation http://wiki.roblox.com/index.php?title=Global_namespace/String_manipulation&redirect=no#string.format
Wiki - Format string http://wiki.roblox.com/index.php?title=Format_string |
|
|
| Report Abuse |
|
|
JetUpNy
|
  |
| Joined: 18 Feb 2016 |
| Total Posts: 258 |
|
|
| 17 Oct 2016 08:31 AM |
I get it but I don't. When I use print I see it in output but how do I get the wonderful goodness of string.format into mzTimeStatus.Value = " timer:: "..i?
print(string.format("%.2d:%.2d",tim/60,tim%60)mzTimeStatus.Value = " timer:: "..i)??
print(string.format(mzTimeStatus.Value = " timer:: "..i "%.2d:%.2d:%.2d",2,45,9))
mzTimeStatus.Value = " timer:: "..i
|
|
|
| Report Abuse |
|
|
JetUpNy
|
  |
| Joined: 18 Feb 2016 |
| Total Posts: 258 |
|
|
| 17 Oct 2016 09:01 AM |
| mzTimeStatus.Value = " timer:: "..i..(string.format("%.2d:%.2d:%.2d",hour,min,sec)) Sorta works...anyways..Thanks all...I'm going to skip this for now. I'm kind of bummed..lol I can't move through my ideas as fast as I'd like to but hopefully it will all make sense in the future. So many ways to do everything..but no simple way. Thanks again..I have somewhere to comeback to. |
|
|
| Report Abuse |
|
|
|
| 17 Oct 2016 09:24 AM |
| you don't need the i i guess but add it after its formatted |
|
|
| Report Abuse |
|
|
JetUpNy
|
  |
| Joined: 18 Feb 2016 |
| Total Posts: 258 |
|
|
| 17 Oct 2016 10:43 AM |
| The i is needed to make the counter work. |
|
|
| Report Abuse |
|
|
JetUpNy
|
  |
| Joined: 18 Feb 2016 |
| Total Posts: 258 |
|
|
| 18 Oct 2016 08:41 AM |
| Back to it. I think I'll try using format.string in a script inside of a textLabel but that seems like a cop out. lol :( but I want to get this done in a hurry. |
|
|
| Report Abuse |
|
|
|
| 18 Oct 2016 08:45 AM |
just convert your seconds appropriately into those values, but make sure to use the actual seconds instead of the new seconds.
and then if you use the format, it formats into 2 ints+ : 2 ints+ : 2 ints+
00:00:00 15:16:02
if you want the hours to start at 0 and not 00 then use .1 or 1 or nothing rather than .2 on the first match 0:29:06 |
|
|
| Report Abuse |
|
|
JetUpNy
|
  |
| Joined: 18 Feb 2016 |
| Total Posts: 258 |
|
|
| 20 Oct 2016 08:33 PM |
| I figured out both with all of your guidance and a lot of reading and experimenting. The end result is pretty simple and I over thought it for sure but it's a learning process and an interesting one. Thanks guys!! |
|
|
| Report Abuse |
|
|