#Detect day or night

1 messages · Page 1 of 1 (latest)

onyx wraith
#

How can I detect when it is day or night? with script (without experimental)

twin jewel
#
import { world } from "@minecraft/server";

const getTimesOfDay = () => {
  var ticks = world.getTimeOfDay();
  if (ticks >= 12000 && ticks <= 24000) {
    return "Night";
  }
  return "Day";
}

If I'm not mistaken, this should work.

#

@onyx wraith

onyx wraith
twin jewel
onyx wraith
twin jewel
#

Wait, what exactly do you want? Spawning a mob or sending messages to chat "Day"/"Night"?

onyx wraith
twin jewel
#

ok

#

Use it function

import { world } from "@minecraft/server";

function RunCmdDayOrNight(player) {
  var ticks = world.getTimeOfDay();
  if (ticks >= 12000 && ticks <= 24000) {
    // Night
    return player.runCommandAsync("Command");
  }
  // Day
  return player.runCommandAsync("Command");
}
#

@onyx wraith

twin jewel
onyx wraith
twin jewel
#

or

#

hm

onyx wraith
#

I did that as a test, it works, but I don't want a function to be called, you understand? The idea is that it works in the same event.

twin jewel
#

hm

#

this is easy

#

sorry

#

i dont speak English

onyx wraith
twin jewel
#

Russian bao_bee_happy

onyx wraith
twin jewel
#

okey

#

just paste the function code in the place where you need to use it.

twin jewel
#

hehe

onyx wraith
twin jewel
#

cool

onyx wraith
# twin jewel cool

It doesn't work anyway lol, I mean it works as you say, but it doesn't work for me like that, it would be that it is always active and for that I would have to keep the other event active and I can't xd, I just want it after the player has completed the tutorial every At night mobs appear in certain locations and when it is day I kill them, for that I need repeated detection

#

idk if you understand me

twin jewel
#

So, does it work or not?

onyx wraith
#

yes, work

onyx wraith
twin jewel
#

Always look at the documentation

#

Function get Random Number in range

const randInt = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
twin jewel
#

wait

onyx wraith
#

detection of the day

#

is unnecessary

twin jewel
#

delete ticks >= 24000

#

How does it even work with this condition?

#

if work - ok

onyx wraith
twin jewel
#

or

#

replace this with ticks >=0

#

yeah

onyx wraith
#

system.runInterval(() => {
  var ticks = world.getTimeOfDay();
  if (ticks >= 12000 && ticks <= 24000 && detection_day == 1) {
    world.getDimension("overworld").runCommand("/say noche")
    return 
  }
  else if (ticks <= 12000 && detection_day == 1) {
    world.getDimension("overworld").runCommand("/say noche")
    return 
  }
})```
#

how?

twin jewel
#

you can do it like that

twin jewel
#

didn't want to complicate things

onyx wraith
#

yep workd

#

thanks!!

twin jewel