#LAG READER

1 messages · Page 1 of 1 (latest)

past jetty
#

indeed the lag was read

idle tartan
#
// Lag Reader
let lastTickTime = Date.now();
function lagReaderServerMs() {
 const currentTime = Date.now();
 const tickDuration = currentTime - lastTickTime;
 lastTickTime = currentTime;
 const expectedTickDuration = 50; // 1 tick = 50 ms
 const lag = tickDuration - expectedTickDuration;
 return { lag }
}
let checkTime = Date.now();
let currentTicks = 0;
let tps = 0;
function lagReaderTps() {
 currentTicks++;
 const currentTime = Date.now();
 if (currentTime - checkTime > 970) {
  tps = currentTicks;
  checkTime = currentTime;
  currentTicks = 0;
 }
 return { tps }
}
system.runInterval(() => {
 const lag = lagReaderServerMs().lag;
 const tps = lagReaderTps().tps;
 const color = ['§a', '§e', '§g', '§6', '§c', '§4'];
 console.warn(`Server: ${Math.floor(lag / 10) >= color.length ? '§4' : color[Math.floor(lag / 10)]}${lag} ms §f| Ticks/s: §b${tps}`);
}, 1);
low pond
#

Is there any way to make this work for every individual player in the server and display it on their actionbar?

chilly plank
low pond
idle elk
low pond
#

I’m getting 240 ticks/s

#

// Lag Reader
let lastTickTime = Date.now();
function lagReaderServerMs() {
    const currentTime = Date.now();
    const tickDuration = currentTime - lastTickTime;
    lastTickTime = currentTime;
    const expectedTickDuration = 50; // 1 tick = 50 ms
    const lag = tickDuration - expectedTickDuration;
    return { lag }
}
let checkTime = Date.now();
let currentTicks = 0;
let tps = 0;
function lagReaderTps() {
    currentTicks++;
    const currentTime = Date.now();
    if (currentTime - checkTime > 970) {
        tps = currentTicks;
        checkTime = currentTime;
        currentTicks = 0;
    }
    return { tps }
}
system.runInterval(() => {
    for (const player of world.getPlayers()) {
        const lag = lagReaderServerMs().lag;
        const tps = lagReaderTps().tps;
        const color = ['a', 'e', 'g', '6', 'c', '4'];
        const coins = world.scoreboard.getObjective("coins").getScore(player.scoreboardIdentity)
        const kills = world.scoreboard.getObjective("kills").getScore(player.scoreboardIdentity)
        const deaths = world.scoreboard.getObjective("deaths").getScore(player.scoreboardIdentity)
        player.onScreenDisplay.setActionBar(`§eCoins: ${coins} \n§aKills: ${kills}\n§cDeaths: ${deaths}\n§${Math.floor(lag / 10) >= color.length ? color[Math.floor(lag / 10)] : '4'}${Math.abs(lag)} ms §f| Ticks/s: §b${tps}`);
    }
});```
#

Why am i getting over 10 times the expected tick/s of 20

#

At some point I had 340 as well

#

Uh

#

UHHHH

#

IT MULTIPLIES FOR EVERY PLAYER IN MY WORLD

#

HOW DO I FIXXX

#

And my ms is dropping to 1-5 sometimes

#

From 50

idle elk
low pond
idle elk
#

you should do world.getplayers()[0] not for (const player of world.getplayers())

low pond
#

But then the rest of the actionbar wont work properly.

idle elk
low pond
#

It’s at 140 right now

#

160 now

#

Because someone just joined

idle elk
#

without getting the lag multiplied by the players

low pond
#

By default i mean

idle elk
#

no they have nothing to do with players

low pond
#

Oh wait I’m dumb

idle elk
#

they re only about the server ms and tps not the players

low pond
#

I’ll only calculate the tps separately

idle elk
#

what?

low pond
#

Now my system.runInterval looks like this system.runInterval(() => { const tps = lagReaderTps().tps; for (const player of world.getPlayers()) { const lag = lagReaderServerMs().lag; const color = ['a', 'e', 'g', '6', 'c', '4']; const coins = world.scoreboard.getObjective("coins").getScore(player.scoreboardIdentity) const kills = world.scoreboard.getObjective("kills").getScore(player.scoreboardIdentity) const deaths = world.scoreboard.getObjective("deaths").getScore(player.scoreboardIdentity) player.onScreenDisplay.setActionBar(`§eCoins: ${coins} \n§aKills: ${kills}\n§cDeaths: ${deaths}\n§${Math.floor(lag / 10) >= color.length ? color[Math.floor(lag / 10)] : '4'}${Math.abs(lag)} ms §f| Ticks/s: §b${tps}`); } });

#

The const tps is outside the for loop

#

Right?

idle elk
idle elk
low pond
#

Also the code had an issue where the ms would show undefined-50 (where 50 was the ms) so I fixed it by switching the ? : logic

#

And the ms was negative so i had to do Math.abs on it

#

But now that you said that it doesn’t work for each player I think that those errors were MY fault

idle elk
#

i think you messed up, math abs wont fix math issues

low pond
#

So, theoretically, this should work system.runInterval(() => { const lag = lagReaderServerMs().lag; const tps = lagReaderTps().tps; const color = ['a', 'e', 'g', '6', 'c', '4']; for (const player of world.getPlayers()) { const coins = world.scoreboard.getObjective("coins").getScore(player.scoreboardIdentity) const kills = world.scoreboard.getObjective("kills").getScore(player.scoreboardIdentity) const deaths = world.scoreboard.getObjective("deaths").getScore(player.scoreboardIdentity) player.onScreenDisplay.setActionBar(`§eCoins: ${coins} \n§aKills: ${kills}\n§cDeaths: ${deaths}\n§${Math.floor(lag / 10) >= color.length ? color[Math.floor(lag / 10)] : '4'}${Math.abs(lag)} ms §f| Ticks/s: §b${tps}`); } });

#

And then I’ll fix any issues with displaying it if they show up

#

Well I just did that and removed the abs and now my ms is having a seziure

#

And not changing colors

#
    const tps = lagReaderTps().tps;
    const lag = lagReaderServerMs().lag;
    const color = ['a', 'e', 'g', '6', 'c', '4'];
    for (const player of world.getPlayers()) {
        const coins = world.scoreboard.getObjective("coins").getScore(player.scoreboardIdentity)
        const kills = world.scoreboard.getObjective("kills").getScore(player.scoreboardIdentity)
        const deaths = world.scoreboard.getObjective("deaths").getScore(player.scoreboardIdentity)
        player.onScreenDisplay.setActionBar(`§eCoins: ${coins} \n§aKills: ${kills}\n§cDeaths: ${deaths}\n§r${lag} ms §f| Ticks/s: §b${tps}`);
    }
});
//§${Math.floor(lag / 10) >= color.length ? color[Math.floor(lag / 10)] : '4'}```
low pond
#

Now that I added a condition that makes it never go below 0 and the coloring back it just looks like I have extremely good connection 🤔