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
- First, create a script under “ServerScriptService”.
- Create a while loop. You’ll need this so that time changes. Here’s what I’ll be doing:
- 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:
- 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:
- 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:
- 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
-
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:
-
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:
- 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!