#Apply knockback in a radius

1 messages · Page 1 of 1 (latest)

icy walrus
#
world.afterEvents.itemUse.subscribe((result) => {
  if (result.itemStack.typeId != "ninjago:item") return;
  for (const player of world.getAllPlayers()) {
    if (player.id == result.source.id) continue;
    player.applyKnockback(Math.random(0, 1), 5, 5);
  }
});

I've gotten this and it doesn't really work the way I want it to.ive checked the wiki but I don't see anything about a radius. Will increasing the horizontal/vertical strength increase radius as well

west sorrel
#

No, what radius are you talking about? in a operation like applyknockback there is no such thing as a radius, are you trying to make a smash attack where entity’s in a radius are knocked around?

west sorrel
#

Then what you are going to want to do is where you have the function that gets all of the players in the world you are going to make the game get all entity’s from the source of the events dimension, adding query options of r, rm, location to target all entities around the one that triggered the event, after which in your loop you apply knockback to each one

icy walrus
west sorrel
#

If you call that exhausting I was just suggesting changing one line of code to add the radius system but ok, if I get a chance I will look into more but just looking at it, it looks fine so not exactly sure what the issue is

modest locust
#
world.afterEvents.itemUse.subscribe((result) => {
    if (result.itemStack.typeId != "ninjago:item") return;
    for (const player of world.getAllPlayers()) {
        if (player.id == result.source.id) continue;

        // change radius here
        const radius = 5;

        const entities = source.dimension.getEntities({
            location: source.location,
            maxDistance: radius
        }).forEach((entity) => {
            entity.applyKnockback(Math.random(0, 1), 5, 5);
        });
    }
});
#

should work

#

@icy walrus

#

btw I noticed a mistake, you used Math.random(0, 1) but that's not how it works, this function does not accept parameters like molang

#

the correct thing to do would be to use Math.random() which returns a random value between 0 and 1

icy walrus
modest locust
icy walrus
modest locust
icy walrus
# modest locust maybe, because it seems kind of unnecessary

Okay I've tested with different items and the items are not the problem,

world.afterEvents.itemUse.subscribe((result) => {
    if (result.itemStack.typeId != "ninjago:shockwave_element") return;
    for (const player of world.getAllPlayers()) {

        const radius = 5;

        const entities = source.dimension.getEntities({
            location: source.location,
            maxDistance: radius
        }).forEach((entity) => {
            entity.applyKnockback(Math.random(0, 1), 5, 5);
        });
    }
});
modest locust
#

@icy walrus you haven't fixed this yet

icy walrus
#

Oh, forgot Abt that

icy walrus
#

Didn't work

modest locust
# icy walrus Didn't work
world.afterEvents.itemUse.subscribe((result) => {
    if (result.itemStack.typeId !== "ninjago:shockwave_element") return;

    const radius = 5;
    const player = result.source;

    const entities = player.dimension.getEntities({
        location: player.location,
        maxDistance: radius
    });

    entities.forEach((entity) => {
        entity.applyKnockback(Math.random(), Math.random(), 5, 5);
    });
});
#
  • source was not defined
  • Math.random(0, 1) was wrong
  • the for loop was useless
#

ig it should work now

muted jasper
modest locust
#

edited

modest locust
#

so yes

icy walrus
#

So minimum distance will exclude an area ?

icy walrus
# modest locust iirc its `minDistance`

Okay so I combined a few things you gave me a few days ago, a long with this and it doesn't work (is it the let nearEntities?, is it the event.source, and should that be changed to event.sender?)

import {
    world,
    system
} from "@minecraft/server"

world.afterEvents.itemUse.subscribe(({
    itemStack,
    source
}) => {
    let hold = false;
    if (itemStack.typeId === 'ninjago:shockwave_element' && !hold && !source.isSneaking) {
        hold = true;

        const shockwaveScore = world.scoreboard.getObjective("shockwave").getScore(source);
        const chiScore = world.scoreboard.getObjective("chi").getScore(source);

        if (shockwaveScore === 1 && chiScore >= 20) {
            let nearEntities = player.dimension.getEntities({
  maxDistance: 2,
  location: player.location
}).filter((player) => player !== event.source);
        });

    entities.forEach((entity) => {
        entity.applyKnockback(Math.random(), Math.random(), 0.6, 5);
    });
});```
modest locust
#

so it should be source.dimension

#

the same for location, should be source.location

modest locust
modest locust
# icy walrus Okay so I combined a few things you gave me a few days ago, a long with this and...
import {
    world,
    system
} from "@minecraft/server"

world.afterEvents.itemUse.subscribe(({
    itemStack,
    source
}) => {
    let hold = false;
    if (itemStack.typeId === 'ninjago:shockwave_element' && !source.isSneaking && !hold) {

        const shockwaveScore = world.scoreboard.getObjective("shockwave").getScore(source);
        const chiScore = world.scoreboard.getObjective("chi").getScore(source);

        if (shockwaveScore === 1 && chiScore >= 20) {
            let nearEntities = source.dimension.getEntities({
                maxDistance: 2,
                location: source.location
            }).filter((entity) => entity.id !== source.id);

            nearEntities.forEach((entity) => {
                entity.applyKnockback(Math.random(), Math.random(), 0.6, 5);
            });
        }
    }
    hold = true;
});
#

also btw, what is the reason for the hold variable? It is being reset every time the event is executed. Is it to do a cooldown or something? Because if so, the reason is kind of useless

icy walrus
icy walrus
modest locust
icy walrus
modest locust
#

just get view direction x and z

#
import {
    world,
    system
} from "@minecraft/server"

world.afterEvents.itemUse.subscribe(({
    itemStack,
    source
}) => {
    let hold = false;
    if (itemStack.typeId === 'ninjago:shockwave_element' && !source.isSneaking && !hold) {

        const shockwaveScore = world.scoreboard.getObjective("shockwave").getScore(source);
        const chiScore = world.scoreboard.getObjective("chi").getScore(source);

        if (shockwaveScore === 1 && chiScore >= 20) {
            let nearEntities = source.dimension.getEntities({
                maxDistance: 2,
                location: source.location
            }).filter((entity) => entity.id !== source.id);

            nearEntities.forEach((entity) => {

const dir = entity.getViewDirection();
                entity.applyKnockback(dir.x, dir.z, 0.6, 5);
            });
        }
    }
    hold = true;
});```
#

if you want the knockback backwards, change the 0.6 to be negative