#Script to detect redstone signal strength from target block when shot quit working

1 messages · Page 1 of 1 (latest)

ionic chasm
#

I know some things have changed. But I can't find what I need to fix to get this script working again. It's supposed to trigger a function if the player hits a bullseye on a target block.

import { world } from "@minecraft/server"

world.events.projectileHit.subscribe(eventData => {
  const { source, blockHit } = eventData
  if (!source.hasTag("gamer")) return;

  const block = blockHit.getBlock()
  if (!block) return;

  let power = block.getRedstonePower()
  if (power >= 14) return;

  source.runCommandAsync(`function getkey`)
})
chrome geyserBOT
#
Debug Result

There is an error in this [code](#1110654020453937302 message):

<repl>.js:4:19 - error TS2339: Property 'blockHit' does not exist on type 'ProjectileHitEvent'.

4   const { source, blockHit } = eventData
                    ~~~~~~~~

ionic chasm
#

I've changed the code. Here is what I tried last. This says it cannot convert object at line 9.

import { world } from "@minecraft/server"
world.events.projectileHit.subscribe(eventData => {
  const {
    source,
    projectile,
    blockHit
  } = eventData
  if (!source.hasTag("gamer")) return;
  const { block } = blockHit
  let power = block.getRedstonePower()
  if (power < 14) {
    source.runCommandAsync(`function getkey`)
  }
})```
gleaming zephyr
#

Well it says blockHit doesnt exist so it may be a method instead

ionic chasm
#

It used to. I don't know how it changed. They added a beforeEvent and afterEvent to the projectile hit event. Would I need to add that somewhere, maybe.

gleaming zephyr
#

So I had to time travel a bit, but I found the docs for ProjectileHitEvent. blockHit is a method

ionic chasm
#

Fixed it. Here's the new script

import { world } from "@minecraft/server"
world.events.projectileHit.subscribe(eventData => {
  const {
    source
  } = eventData
  if (!source.hasTag("gamer")) return;
  const { block } = eventData.getBlockHit()
  let power = block.getRedstonePower()
  if (power < 14) {
    source.runCommandAsync(`function getkey`)
  }
})```
chrome geyserBOT
#
No Errors

No errors in [code](#1110654020453937302 message)

ionic chasm
#

Argh. It works if load a block full of arrows above the target block. Then break the block and let arrows fall. The command runs. But I can shoot a thousand arrows and it wont run until I drop the arrows.

gleaming zephyr
#

Does it work with other projectiles?

ionic chasm
#

I didnt try.

#

Snowballs do work

gleaming zephyr
#

🤔 Intersting, try asking in #1067535608660107284 if theres a bug with detecting arrows from players. If there isnt, give them a link to this post so they can see why it wont work

ionic chasm
#

Thank you

#

It doesn't detect trident either

gleaming zephyr
#

bao_doggo_derp Interesting, does the trident work if you do the same as arrow, just throw on trapdoor than drop the tridents

ionic chasm
#

I'll try

#

Yep. Same. Dropping trident works

dire steeple
#

@ionic chasm ```js

world.events.projectileHit.subscribe(eventData => {
const {
source,
projectile
} = eventData
if (!source.hasTag("gamer")) return;
const blockHit = eventData.getBlockHit()
let power = blockHit?.block.getRedstonePower()
if (power < 14) {
source.runCommandAsync(function getkey)
}
})```

ionic chasm
#

Didn't work. No errors though

dire steeple
#

power > 14

ionic chasm
#

Let me try

#

Nope. Not sure what's going on. I have a custom block that only tags the player when stepped on. But when I shoot it the ommand runs from the script. It's a custom block with the target texture only.

dire steeple
#

dont do tag

#

run an event on the player instead

#

how is that supposed to work?

ionic chasm
#

The block runs an event that tags the player with "gamer" when stepped on and removes it when step off.
I'll post the addon.

#

That runs everytime I shoot it now.

dire steeple
#

does it really give u the tag?

ionic chasm
#

Yes

#

And removes it when stepped off

#

Idontk ow why when I shoot that block it runs the command even without the tag

dire steeple
#

add a filter in the event

#

just learned that on_step_on triggers when a projectile is attached on the block

ionic chasm
#

Oh. That explains it running when shot.

#

Should I just add to the command that tags the player to detect them standing on the right block?

dire steeple
#

can u change it to @s instead of @p

#

ye, @s works

ionic chasm
#

Sorry im at work still (12hrs ugh). Does changing to @s keep it from adding the tag if the player shoots it?

dire steeple
#

it tags the entity that steps on it

ionic chasm
#

Cool.

dire steeple
#

if the player shoots it, the projectile will get the tag, instead of the source

ionic chasm
#

Nice. Makes sense

ionic chasm
#

OK. I'm still having an issue. If I stand on the "Target Stand" and shoot the target stand the command runs. I can't figure out why it would cause a redstone signal to fire. It doesn't do anything but add the tag.

ionic chasm
#

I fixed the issue with the command running in the function that gives the key. Changed @s to @p so it only does the player. But i can't get it to fire at the correct signal strength.

ionic chasm
#

I just noticed it runs when any block is hit with a projectile.