#apply knockback from entity

1 messages · Page 1 of 1 (latest)

umbral nova
#

i have an entity in my game, i was wondering if i can give players a tag that forces them to take knockback from the source of that main entity. how would i go about this

steady pollen
#

but it should

#
world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack
    const player = data.source
    if (data.itemStack.typeId == 'minecraft:stick') {
        const entityarray = world.getDimension(player.dimension.id).getEntities({type:'minecraft:pig', closest: 1})
        if (entityarray != []) {
            const entity = entityarray[0]
            player.applyKnockback(player.location.x - entity.location.x, player.location.z - entity.location.z, 1, 1)
        }
    }
})
#

waiti forgor smt

#
world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack
    const player = data.source
    if (data.itemStack.typeId == 'minecraft:stick') {
        const entityarray = world.getDimension(player.dimension.id).getEntities({location: player.location ,type:'minecraft:pig', closest: 1})
        if (entityarray != []) {
            const entity = entityarray[0]
            player.applyKnockback(player.location.x - entity.location.x, player.location.z - entity.location.z, 1, 1)
        }
    }
})
umbral nova
#

ok lemme check it out

#

@steady pollen this works! is this something i can have run constantly?

#

and without using an item

#

just constantly checking if a player has a tag

#

like if i have the tag doExplode i get knockbacked from the closest pig

steady pollen
# umbral nova <@661793180550955028> this works! is this something i can have run constantly?
system.runInterval(() => {
    const players = world.getAllPlayers()
    players.forEach((player) => {
        if (player.hasTag('test')) {
            const entityarray = world.getDimension(player.dimension.id).getEntities({location: player.location ,type:'minecraft:pig', closest: 1})
        if (entityarray != []) {
            const entity = entityarray[0]
            player.applyKnockback(player.location.x - entity.location.x, player.location.z - entity.location.z, 1, 1)
        }
        }
    })
})
#

that does it every tick

#

or it should atleast

#

i'd lower it

umbral nova
#

lag?

steady pollen
#

yeah

umbral nova
#

well this seems to work great

#

thank you so muchh

steady pollen
#

np

umbral nova
#

sorry im dumb no more questions

steady pollen
#

at the end of the stsem run interval

#

instead of })

#

do }, <tick delay>)

umbral nova
#

thank you @steady pollen !!

steady pollen
#

np

#

yes

umbral nova
#

:>

steady pollen
#

coolio

umbral nova
#

it like

#

just

#

dawned on me in your pfp

#

you can barely see it but its there

#

IT BROKEE

#

LMFAO WHAT DID I DO

steady pollen
#

is there a pig

umbral nova
#

there was

#

now it is dead

#

OHHH

#

ok i need an alternative solution i can fix this

steady pollen
#

instead of entityarray != []

#

do entityarray[0] != undefined

#

and see if that fixes it

#

did that fix it?

steady pollen
#

did you get the error to go away

steady pollen
umbral nova
#

@steady pollen fixed it

#

:)

steady pollen
#

nice

umbral nova
#

why am i unable to add multiple?

steady pollen
umbral nova
#

just will not work

#

cause i was gonna make a smaller boom

steady pollen
#

you removed a }

#

from if entityarray != []

#

the first one

#

did that fix it?

umbral nova
#

lets seee

steady pollen
#

you have to put a } on the line after player.runCommandAsync('kill @e[name=nadeboom]')

umbral nova
#
system.runInterval(() => {
    const players = world.getAllPlayers()
    players.forEach((player) => {
        if (player.hasTag('c4explode')) {
            const entityarray = world.getDimension(player.dimension.id).getEntities({location: player.location ,type:'minecraft:armor_stand', closest: 1})
        if (entityarray != []) {
            const entity = entityarray[0]
            player.applyKnockback(player.location.x - entity.location.x, player.location.z - entity.location.z, 10, 1.2)
            player.removeTag('c4explode')
            player.runCommandAsync('kill @e[name=c4explode]')
        }
        if (player.hasTag('c4explode2')) {
            const entityarray = world.getDimension(player.dimension.id).getEntities({location: player.location ,type:'minecraft:armor_stand', closest: 1})
        if (entityarray != []) {
            const entity = entityarray[0]
            player.applyKnockback(player.location.x - entity.location.x, player.location.z - entity.location.z, 10, 1.2)
            player.removeTag('c4explode2')
            player.runCommandAsync('kill @e[name=c4explode]')
        }
        }
        }
    })
})
#

i have this rn

steady pollen
#

is it working?

umbral nova
steady pollen
#

what isn't working exactly

umbral nova
#

figured it out i think

#

holup

#

yeahh

#

i had everything inside the first if statement

steady pollen
#

yeah that's what i meant earlier

steady pollen
#

oh yeah i see it now

#

nvm i was being dumb earlier

#

mb