Real Time Clock (UI and Scripting)

INTRODUCTION
With people being interested in creating a real-time game, similar to the Animal Crossing Franchise, I thought to show you all how to create your own in-game clock system that only supports 1 player servers (I may do another tutorial for more than one in the future if this is a success). This is going to teach you about UI (Apologies, in the long run, I can’t provide screenshots) and intermediate scripting. If you are not an intermediate scripter, don’t let that discourage you. You’ll learn to do it! I’m here to help! With that all taken care of, let’s begin!

Server-Sided Scripting

  1. First, create a script under “ServerScriptService”.
  2. Create a while loop. You’ll need this so that time changes. Here’s what I’ll be doing:
  1. Next, we will need to create variables. We are going to need the Time. So, you’ll want to create a variable that gets the current time in Unix. Unix is the number of seconds after January 1st, 1970. Here’s my code:
  1. We have an issue. NOBODY reads in Unix (unless you want to, the player doesn’t want to). So, we will need to convert it. You can read for more info for converting HERE. In our case, we will be using Hours (H), Minutes (M), and Seconds (S). You’ll need to create a variable with those down and have the variable equal to the following: “%H:%M:%S” . Here’s my code:
  1. Now, to actually convert it, we’ll need to use the os.date() function. Inside the parenthesis, there are two parameters: The format you want and the os time. In our case, our first parameter will be “stringToFormat” and our second will be “TimeInUnix” . Here is my code:
  1. Woohoo! We have a working time clock, however, we can’t see it in action. We’re going to use the lighting service to set the TimeOfDay. Create a variable for getting the lighting service and set the TimeOfDay to result as shown in my code:

Woohoo! We have a working clock that you can see based on the lighting. Onto the UI!

Client-sided Coding

  1. Create a ScreenGui in StarterGui. Inside of the ScreenGui, create two text labels. Name one of them the Time and the other one Date. Inside of both Textlabels, create a local script. Here is what I have:
    image

  2. Copy the script that we created as it’s very similar. We will not be using the Lighting service so you can remove those two lines. First the date. Go into the local script and paste the code. To format, we will need to set the Date (A), Month (B), and Year (d). We will need to set the text equal to the result. Here is my code:

  1. Now we need to set the time (which is easy). Copy the date code and go to the Time’s local script and paste it in there. To format, we will need the Hour (I), Minutes (M), and whether it’s Am or PM (‘p’). Here is my code:

Wohoo! you’ve completed the tutorial. Test the game to see this all in action! I hope this helped a lot of people. Let me know if you need any help! Thank you!

73 Likes

ill try this out later on, rn not working on a game that needs this, good tutorial though, however, is it possible you can provide a gif/video that shows it in action?

3 Likes

I’d love to do that, however, I can’t :frowning: . I do believe how I did set it up was helpful

Good job on your work!

It says my Time, [ETC Florida USA.]

5:19

3 Likes

This works great. I have a question thought, will it show the same time for every player? Like for example, if your timezone is EST and another players timezone is GMT, will it show the same time like 6:24 PM EST?

1 Like

The UI should be good to work for each player. The server-sided stuff won’t be because whoever first joins will determine the time and the lighting.

Maybe that’s why he said this.
oops replaid to wrong person meant to reply @JackTheKrazyGuy

3 Likes

thanks for this tutorial !! this really helps me !

1 Like

This would be so cool for vehicle sims and real world sims like flight sims and stuff. Nice job!

3 Likes

Very cool but I was wondering, I’m making a SurfaceUI clock and is there a way to show if it’s PM or AM?

Yes,

This takes care of that: HH:MM PM/AM. Specifically %p. You can read more about it here: os

The clock works. I really like it in my game, but how to show timezones like est, pst, mst cst, or others?

Yeah this works. It’s pretty nice of Lua to make everything so simple.

Should be able to get that if you use os.date and check the time at that current time for every timezone.

That is awesome i love how its exactly right even on some milliseconds never thought this would even be possible