Angel Posted August 24, 2018 Share Posted August 24, 2018 Hello everyone, I thought I would share this code snippet that others may find useful. It is a purely client side time of day progression script that gives the same time progression as it is in single player. To use it, simply `require` the js file and it automatically starts. Quote (function() { var clockInterval = 0; if (!clockInterval) { var hour = 0; var min = 0; var sec = 0; clockInterval = setInterval(function() { var d = new Date(); var h1 = d.getUTCHours(); var m1 = d.getUTCMinutes(); var s1 = d.getUTCSeconds(); var ms = d.getUTCMilliseconds(); hour = (Math.floor(m1/2) + h1 * 6) %24; min = (Math.floor(s1/2) + m1 * 30) % 60; sec = (Math.floor(ms*0.03) + s1 * 30) % 60; mp.game.time.setClockTime(hour, min, sec); mp.game.time.setClockDate(d.getUTCDate(), d.getUTCMonth(), d.getUTCFullYear()); }, 250); } })(); 3 Link to comment Share on other sites More sharing options...
MrPancakers Posted August 24, 2018 Share Posted August 24, 2018 If possible it's best to run serverside if your whole server depends on everyone having the same time. This code would execute based on that clients current time and not the server time. So someone in America, for example, would have their world in the daytime, but someone playing in Australia would be playing with their world in the nighttime. It's a good share though. Link to comment Share on other sites More sharing options...
Angel Posted August 24, 2018 Author Share Posted August 24, 2018 Actually, if the users have their time and timezone set correct, each client will have the same in game time as the script uses GMT as its reference. I've tested this with two computers running different timezones, in game time is the same. Link to comment Share on other sites More sharing options...
MrPancakers Posted August 24, 2018 Share Posted August 24, 2018 1 hour ago, Angel said: Actually, if the users have their time and timezone set correct, each client will have the same in game time as the script uses GMT as its reference. I've tested this with two computers running different timezones, in game time is the same. You're right my mistake, you're using UTC, I quickly read over and thought you were simply using getHours() etc to grab the computers time. Still great though and this is really useful. 1 Link to comment Share on other sites More sharing options...
Marshmellow Posted January 5, 2023 Share Posted January 5, 2023 A good and instructive resource. Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now