#Detect day or night
1 messages · Page 1 of 1 (latest)
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
and if I want to do a command, let's say if it's night I want a zombie to spawn and if not a cow
Use it function
import { world } from "@minecraft/server";
function spawn(player) {
var ticks = world.getTimeOfDay();
if (ticks >= 12000 && ticks <= 24000) {
return player.dimension.spawnEntity("minecraft:zombie", player.location);
}
return player.dimension.spawnEntity("minecraft:cow", player.location);
}
I meant if I wanted to do a player.runcommandasync('/say it's day') or player.runcommandasync('/say it's night'
Wait, what exactly do you want? Spawning a mob or sending messages to chat "Day"/"Night"?
I want to be able to execute a command, any command
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
How do you use the function?
I already saw how it works, but it still wouldn't work
How do you call the function? Post a screenshot.
or
hm
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.
spanish?
Russian 
ohh i see, so?
I try not to use a translator. So I forget myself a little and I think it’s a mistake.
hehe
dw haha, i speak spanish, so sometimes i try the same
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
So, does it work or not?
yes, work
but I need it to be something like
You can use the function of getting random numbers in a range, spawn mobs in the interval and check if it’s day then kill all the mobs.
Always look at the documentation
Function get Random Number in range
const randInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
i do
thanks you!
wait
in the day dont workd xd
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?
you can do it like that
this condition interfered with the work, so I did not use else
didn't want to complicate things
np