question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

How To Check Time Difference in Cloudscript?

Hello everyone,

I want to implement a logic where the players can interact with once every 5 minutes. To be able to that, I'm using read only data. (I will store the current time of the server when the API is called). My questions are:

- How can get the current time of the server in cloudscript?

- How can check the time difference between the read only data and the current time of the server?

Here's the code I've written.

//...
    var value = lastTimeResult.Data["StreetRace"].Value;
    if(value != "Now")
    {
        var lastTime = new Date(value);
        var now = new Date().getTime();
        
        var difference = (now - lastTime) / 1000;
        if(difference < 60)
        {
            return { "SecondsLeft" : difference }
        }
    }
//... the rest of the code
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Seth Du avatar image
Seth Du answered

Cloud Script is based on Google V8 JavaScript engine, which means you can get time via Javascript built-in method Date.getTime().

I believe your idea in the codes should be correct. After the verification is passed, you may need to update PlayerData with the current server time. In the next verification step, you can get timestamp in Player Data and call Date.getTime() to retrieve current timestamp, then make the comparison.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.