Hey there, Looking for a time spent in server script.

[BHE] LewisJ
Joined 26/02/2021
Posts 3,681
12:47 PM 08/01/2023
So for example if i spend 1 minute ingame my score would change to 1 and so on, Could anyone help please?


Dasfk
Joined 06/11/2020
Posts 28
02:09 PM 08/01/2023
well you could do smth likevar start = new Date().getTime(),
score = '0.1';

var interval = window.setInterval(function() {
var time = new Date().getTime() - start;

score = Math.floor(time / 1000);

if(score === 100) {
window.clearInterval(interval);
if(!alert("You win!\nPress 'OK' to play again")){
window.location.reload();
}
}

document.getElementById('displayScore').innerHTML = score += '.00 Score';
});


dargy
Joined 23/06/2019
Posts 4,820
07:17 PM 08/01/2023
Quote from Dasfk , 02:09 PM 08/01/2023
well you could do smth likevar start = new Date().getTime(),
score = '0.1';

var interval = window.setInterval(function() {
var time = new Date().getTime() - start;

score = Math.floor(time / 1000);

if(score === 100) {
window.clearInterval(interval);
if(!alert("You win!\nPress 'OK' to play again")){
window.location.reload();
}
}

document.getElementById('displayScore').innerHTML = score += '.00 Score';
});
Stop copy and pasting stuff from StackOverflow, this isn't a website scripting forum.

Besides, there is a way easier way to make this with only 5 lines of code. Let me break it down.

On playerJoin, we set a p.setInterval. Why are we using a p.setInterval? Because when the player is destroyed/left, the p.setInterval automatically clears itself instead of a normal setInterval continuing to run when the player is destroyed. So now, every 60000 miliseconds (1000 miliseconds = 1 second, so 60 seconds), we increase the player's score. Simple as that.

Script: https://gist.github.com/dragonismcode/dfcc1e88d50b9fe03ece3b057bf33a65

Hope this helps!


[POST] GottaScooby
Joined 09/12/2021
Posts 1,607
02:15 AM 19/01/2023
Quote from dargy , 07:17 PM 08/01/2023
Quote from Dasfk , 02:09 PM 08/01/2023
well you could do smth likevar start = new Date().getTime(),
score = '0.1';

var interval = window.setInterval(function() {
var time = new Date().getTime() - start;

score = Math.floor(time / 1000);

if(score === 100) {
window.clearInterval(interval);
if(!alert("You win!\nPress 'OK' to play again")){
window.location.reload();
}
}

document.getElementById('displayScore').innerHTML = score += '.00 Score';
});

Stop copy and pasting stuff from StackOverflow, this isn't a website scripting forum.

Besides, there is a way easier way to make this with only 5 lines of code. Let me break it down.

On playerJoin, we set a p.setInterval. Why are we using a p.setInterval? Because when the player is destroyed/left, the p.setInterval automatically clears itself instead of a normal setInterval continuing to run when the player is destroyed. So now, every 60000 miliseconds (1000 miliseconds = 1 second, so 60 seconds), we increase the player's score. Simple as that.

Script: https://gist.github.com/dragonismcode/dfcc1e88d50b9fe03ece3b057bf33a65

Hope this helps!
i mean.. simplifying code is good and all but imo i use stack overflow


dargy
Joined 23/06/2019
Posts 4,820
01:42 PM 20/01/2023
Quote from GottaScooby , 02:15 AM 19/01/2023
Quote from dargy , 07:17 PM 08/01/2023
Quote from Dasfk , 02:09 PM 08/01/2023
well you could do smth likevar start = new Date().getTime(),
score = '0.1';

var interval = window.setInterval(function() {
var time = new Date().getTime() - start;

score = Math.floor(time / 1000);

if(score === 100) {
window.clearInterval(interval);
if(!alert("You win!\nPress 'OK' to play again")){
window.location.reload();
}
}

document.getElementById('displayScore').innerHTML = score += '.00 Score';
});

Stop copy and pasting stuff from StackOverflow, this isn't a website scripting forum.

Besides, there is a way easier way to make this with only 5 lines of code. Let me break it down.

On playerJoin, we set a p.setInterval. Why are we using a p.setInterval? Because when the player is destroyed/left, the p.setInterval automatically clears itself instead of a normal setInterval continuing to run when the player is destroyed. So now, every 60000 miliseconds (1000 miliseconds = 1 second, so 60 seconds), we increase the player's score. Simple as that.

Script: https://gist.github.com/dragonismcode/dfcc1e88d50b9fe03ece3b057bf33a65

Hope this helps!

i mean.. simplifying code is good and all but imo i use stack overflow
nothing wrong with copy and pasting code from StackOverflow, just he didn't even think about what type of code he was pasting

1