Discord
Login
Community
DARK THEME

How would I get an effect like " time.sleep " in microstudio?

Or even "wait()" in roblox studio

I would advise you not to use this concept because your program pauses and nothing else happens in this time. Think about using timers. Store the timestamp of an event and compare to current time (system.time()). If the difference is big enought then do sth.

For example print „Hello“ 1 second after clicking.

init = function()
  lastPressTime = 0
  duration = 1000 // milliseconds
end

update = function()
  
  if touch.press then
    lastPressTime = system.time() // store timestamp of the press event
  end
  
  if (lastPressTime > 0) and (system.time() - lastPressTime > duration) then
    print("Hello")
    lastPressTime = 0
  end

end
//…

If you're using MicroScript, there's also the after keyword. It starts a new asynchronous thread that lets the rest of the project keep running.

init = function()
  thread = after 1 second do
    print("1 second after init")
  end
  print("end of init")
end

Result (1 second between logs):

end of init
1 second after init

You can also make a basic timer tracker.

timers = object end
setTimer = function(delay, callback)
  timers[system.time() + delay] = callback
end

init = function()
  setTimer(1000, function()
    print("one")
  end)
  setTimer(2000, function()
    print("two")
  end)
  setTimer(3000, function()
    print("three")
  end)
  
  print("end of init")
end

update = function()
  for time in timers
    if system.time() >= time then
      timers[time]()
      delete timers[time]
    end
  end
end

Result (1 second between logs):

end of init
one
two
three

Thanks for the insight.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community