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}`)
}
}
#It's working and yet the script is giving an error, what's the logic?
1 messages · Page 1 of 1 (latest)
change to player?.name
?
@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}`);
}
});
nice thankys
Isn't it better if I just use:
if(player.typeId == "minecraft:player"){
var playerName = player.name
}
Doesn't really matter. I wouldn't use var in that example though. Practice using let or const.
However, my example, or your example, will do the job.
Is there any difference between me using let, var and const? Please explain the differences to me.
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)
nice, very thankys!
honestly, var should really only be used for function stuff imo. the redeclaring/assigning part can be replaced by let
Except, if you aren't redeclaring var then you would typically (or should IMHO) be initializing and declaring it as a const.
yeah
but i was just saying this for your var example
The examples were solely intended to show the difference in behaviors between using const, let, and var so PinhoPlays could have a visual understanding of what I was trying to talk about when they asked what the difference was between them.
i know. and i just wanted to state my opinion on your var example, i understand they were meant for PinhoPlays.
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