generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
We use cookies to offer you a better experience. By using Roblox.com, you are agreeing to our Privacy and Cookie Policy.
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: How can I keep track of time in a script?

Previous Thread :: Next Thread 
Twistir is not online. Twistir
Joined: 19 Nov 2009
Total Posts: 12374
15 Nov 2015 07:50 PM
I have a coroutine set to go off when there is one item left in a table, or if certain named items are in the table. I also need it to activate when x amount of time has passes, how can I do it? This is a basic example of the code:

function OneItem()
print('One item left')
end

function MultipleItems()
print('More than one item left')
end

function TimeRanOut()
print('Time ran out')
end

coroutine.resume(coroutine.create(function()
while wait() do
if #items == 1 then
local item = coroutine.wrap(OneItem);item();
elseif #items >= 1 then
local multitem = coroutine.wrap(MultipleItems);multitem();

****I NEED THE TIME PART SOMEWHERE HERE****

end
end
end))
Report Abuse
Twistir is not online. Twistir
Joined: 19 Nov 2009
Total Posts: 12374
15 Nov 2015 08:27 PM
bump
Report Abuse
Twistir is not online. Twistir
Joined: 19 Nov 2009
Total Posts: 12374
15 Nov 2015 08:44 PM
Do I need to explain more?
Report Abuse
Twistir is not online. Twistir
Joined: 19 Nov 2009
Total Posts: 12374
15 Nov 2015 10:25 PM
anyone?
Report Abuse
goro7 is not online. goro7
Joined: 01 Jul 2009
Total Posts: 735
15 Nov 2015 10:27 PM
tick() returns local time, time() returns server time.

http://wiki.roblox.com/index.php?title=Function_dump/Functions_specific_to_ROBLOX#tick
Report Abuse
Twistir is not online. Twistir
Joined: 19 Nov 2009
Total Posts: 12374
15 Nov 2015 10:29 PM
Not exactly what I want, I would like to use wait()
Report Abuse
goro7 is not online. goro7
Joined: 01 Jul 2009
Total Posts: 735
15 Nov 2015 10:43 PM
Wait() returns a float of time in seconds that was waited.
Report Abuse
Listwer is not online. Listwer
Joined: 25 Jul 2012
Total Posts: 151
15 Nov 2015 10:48 PM
elapsedTime()

/\ This returns how many seconds the program has been running for.

So if you make a start variable and then subtract it from the function elapsedTime(), you will get the amount of time since you made that start variable.

local start = elapsedTime()

wait(5)

local timepassed = elapsedTime()-start

print(timepassed)

Which should output around 5.

goro7's suggestion should work also, but both of those only update during wait(), and they are also larger numbers. That shouldn't matter, but it might in special cases.
Report Abuse
Twistir is not online. Twistir
Joined: 19 Nov 2009
Total Posts: 12374
15 Nov 2015 10:59 PM
"Around 5" So it won't be exact? Is it local or server sided?
Report Abuse
Listwer is not online. Listwer
Joined: 25 Jul 2012
Total Posts: 151
15 Nov 2015 11:10 PM
It's local sided.

You have to take into account the calculation time that the computer takes to calculate every line of code. You also have to take into account random variances in roblox's update speed.

So if I were to run this:

local start = elapsedTime()

wait(5)

local timepassed = elapsedTime()-start

print(timepassed)


I get about 5.008 seconds returned.

But when I run this:

local start = elapsedTime()

wait(5)

local timepassed = elapsedTime()-start

print(elapsedTime()-start) --Cutting down calculation time by not assigning a variable to a calculation's value.


I get about 5.005 seconds returned.

These two variances could have just been random update ticks, but 0.008 seconds isn't that much time, you will pretty much be fine. 0.008 seconds is faster than the refresh rate of a 120hz monitor for comparison.
Report Abuse
Listwer is not online. Listwer
Joined: 25 Jul 2012
Total Posts: 151
15 Nov 2015 11:15 PM
** And rounding errors
Report Abuse
Listwer is not online. Listwer
Joined: 25 Jul 2012
Total Posts: 151
15 Nov 2015 11:20 PM
**** Second Function
local start = elapsedTime()

wait(5)

print(elapsedTime()-start) --Cutting down calculation time by not assigning a variable to a calculation's value.




I really am messing up today lol :P
Report Abuse
Twistir is not online. Twistir
Joined: 19 Nov 2009
Total Posts: 12374
15 Nov 2015 11:27 PM
Yeah I need something that is server sided, this won't do :/
Report Abuse
CloneTrooper1019 is not online. CloneTrooper1019
Joined: 19 Jan 2009
Total Posts: 6227
15 Nov 2015 11:31 PM
So you want it to trigger literally on the dot when the time reaches that point?
Its hard to measure 100% precise time in Lua, sometimes you don't need it to be that precise.

What exactly is wrong with tick() or elapsedTime()?
Report Abuse
Twistir is not online. Twistir
Joined: 19 Nov 2009
Total Posts: 12374
15 Nov 2015 11:49 PM
I don't think anyone is understand what I originally posted, so I'm going to try and explain again. I am using coroutine.resume to find when a player wins or time runs out. I need help with the time part, as I can't figure out how to set a timer in the coroutine.
Report Abuse
CloneTrooper1019 is not online. CloneTrooper1019
Joined: 19 Jan 2009
Total Posts: 6227
15 Nov 2015 11:58 PM
local con
local rs = game:GetService("RunService")
local timeLeft = 30
local isRunning = true
local items = {"????","Whatever you have in here."}

local function update()
if #items == 0 then
print("All items collected!")
con:disconnect()
isRunning = false
elseif #items == 1 then
print("One Item Left")
else
print("More than one item left.")
end
end

con = rs.RenderStepped:connect(update)

while timeLeft > 0 do
if not isRunning then
break
else
timeLeft = timeLeft - 1
wait(1)
end
end

if isRunning then
isRunning = false
print("Time ran out!")
con:disconnect()
end

print("Done?")
Report Abuse
CloneTrooper1019 is not online. CloneTrooper1019
Joined: 19 Jan 2009
Total Posts: 6227
15 Nov 2015 11:59 PM
Hang on whoops
Change that from RenderStepped to Stepped if you are using a normal script.
Report Abuse
Twistir is not online. Twistir
Joined: 19 Nov 2009
Total Posts: 12374
16 Nov 2015 12:09 AM
Im a bit confused. Where would that go in terms of the script? Inside the coroutine?
Report Abuse
CloneTrooper1019 is not online. CloneTrooper1019
Joined: 19 Jan 2009
Total Posts: 6227
16 Nov 2015 12:10 AM
Stop using coroutines.
Its that simple.
Report Abuse
Twistir is not online. Twistir
Joined: 19 Nov 2009
Total Posts: 12374
16 Nov 2015 12:18 AM
How else am I suppose to find when certain requirements are met in a script and then repeat the process...
Report Abuse
CloneTrooper1019 is not online. CloneTrooper1019
Joined: 19 Jan 2009
Total Posts: 6227
16 Nov 2015 12:21 AM
...
Look at the script I just wrote.
Run it.
Realize it does what you want it to.

Is there something I'm not getting here?
Report Abuse
CloneTrooper1019 is not online. CloneTrooper1019
Joined: 19 Jan 2009
Total Posts: 6227
16 Nov 2015 12:21 AM
...
Look at the script I just wrote.
Run it.
Realize it does what you want it to.

Is there something I'm not getting here?
Report Abuse
Twistir is not online. Twistir
Joined: 19 Nov 2009
Total Posts: 12374
16 Nov 2015 12:22 AM
So abandon the whole coroutine concept then? Renderstepped is more efficient?
Report Abuse
CloneTrooper1019 is not online. CloneTrooper1019
Joined: 19 Jan 2009
Total Posts: 6227
16 Nov 2015 12:35 AM
I meant to change it to Stepped.
The Stepped event fires 30 times a second in a seperate thread
Report Abuse
Twistir is not online. Twistir
Joined: 19 Nov 2009
Total Posts: 12374
16 Nov 2015 12:38 AM
Alright
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image