#It's working and yet the script is giving an error, what's the logic?

1 messages · Page 1 of 1 (latest)

uneven marlin
#
world.afterEvents.projectileHitEntity.subscribe(ev => {
  const { source: player, projectile } = ev;
  const entity = ev.getEntityHit().entity
  const playerName = player.name

  if(player.typeId == "minecraft:player"){
    if (projectile.typeId == "blc:fragor_lan") {
      player.runCommand(`say aa ${playerName}`)
    }
  }
#

what happens when i attack any entity

sick juniper
#

change to player?.name

uneven marlin
#

?

boreal cliff
#

@uneven marlin @sick juniper

source returns Entity and not Player. This means that it could be a player or an entity. The property for .name only exists from the Player class. If it's an entity that isn't a player, then .name doesn't exist, and you must use .nameTag instead.

Would look something like this:

import { world, Player } from "@minecraft/server";

world.afterEvents.projectileHitEntity.subscribe(ev => {
  const { source, projectile } = ev;
  const entity = ev.getEntityHit().entity;

  // If source is a player and the projectile is of type "blc:fragor_lan"
  if (source instanceof Player && projectile.typeId === "blc:fragor_lan") {
    const playerName = source.name;
    source.runCommand(`say aa ${playerName}`);
  }
});
uneven marlin
boreal cliff
uneven marlin
boreal cliff
#

const means the variable is constant. It has no intentions of being changed. Any time you have variables in your code which you do not change or do not intend to change then you use const to initialize and declare the variable. This ensures that if you or a partner attempts to write to that variable later on in the code, your IDE of your choice would notify you with an error saying that your constant variable doesn't have write permissions therefore it cannot be written to. The game will let you know too.

If your variable needs to be written to later on in the script then you use let. let helps to ensure the variable is maintained within the scope of your code. It prevents other codes outside the scope from overwriting it by accident.

var makes the variable global within the construct of your script. This means it doesn't matter what scope it's in, you can call the variable, and modify it accordingly. This can open the door for messy code and poor management if you do not stay cognitively aware of any global variables used.

#

Here are some examples:

const

const playerName = "Alex";
console.log(playerName); // Alex

// Trying to change the value will cause an error
playerName = "Steve"; // Error: Assignment to constant variable

let

let score = 0;
console.log(score); // 0

score = 10;
console.log(score); // 10

if (true) {
  let level = 1;
  console.log(level); // 1 (within this block)
}

console.log(level); // Error: level is not defined (outside the block)

var

var lives = 3;
console.log(lives); // 3

lives = 2;
console.log(lives); // 2

if (true) {
  var powerUp = "Shield";
  console.log(powerUp); // Shield (within the block)
}

console.log(powerUp); // Shield (still accessible outside the block because var is function-scoped)
marsh narwhal
boreal cliff
marsh narwhal
#

but i was just saying this for your var example

boreal cliff
marsh narwhal
sick juniper
# uneven marlin ?
world.afterEvents.projectileHitEntity.subscribe(ev => {
  const { source: player, projectile } = ev;
  const entity = ev.getEntityHit().entity

  if(player?.typeId == "minecraft:player"){
    const playerName = player.name
    if (projectile.typeId == "blc:fragor_lan") {
      player.runCommand(`say aa ${playerName}`)
    }
  }```
this is how to fix it literally