#Constantly running a command

1 messages · Page 1 of 1 (latest)

night talon
#


    system.runInterval(() => {
        let player = event.source
        player.runCommandAsync("effect @s night_vision 99 99")
    }
        , 40
    )
})``` I've got this little snippet of code, everything that needs importing has been done already as well, but when I join the world, it says cannot read property runCommandAsync. It's porbably a small oversite but I am to tired to think straight.
inner zenith
#

The properties on PlayerJoinAfterEvent are 'playerId' and 'playerName'. There isn't a 'source' property, like you are trying to access, so the player variable is undefined

#

With world.getEntity() you could acquire the player, then access the runCommandAsync() method on that. But is there a need to subscribe to playerJoin to start with?

#

Would this not work perfectly fine on its own being independent? By looping over all players and doing runCommandAsync on each in turn

night talon
inner zenith
#

Something like this:

system.runInterval(function() {
  world.getAllPlayers().forEach(function applyNightVision(player) {
    player.runCommandAsync("effect @s night_vision 99 99")
  })
}, 40)
#

Although in this case there is an addEffect() method you could use:
player.addEffect('night_vision', 99, {amplifier: 99})

night talon
#

Omg I forgot to put () after getAllPlayers

#

Thanks lol

inner zenith
#

Heh, you're welcome

median locust
#
system.runInterval(()=>{
    world.getAllPlayers().forEach(player=>player.addEffect('night_vision', 99, {amplifier: 99}))
})```
fading osprey
#

@night talon do this one instead^

inner zenith
#

This is identical to what I suggested though.

waxen prism
#

use tick.json?

#

it works better?

night talon
#

tick.json doesn't seem to work with the damage command as far as I can tell

night talon
fading osprey
#

Oh i see, but usually you can do the same with the apis

night talon
#

yeah, and I probably will at some point, but my knowledge of commands far surpasses my knowledge of js with mcbe integration

inner zenith
night talon
inner zenith
#

I get that, I moreso meant sharing your files specifically. To see if there were any typos, or if someone could reproduce it

night talon
#

I tested this in 1.20.80, so they may work now