#[RESOLVED] Setting the default name value to the entity's name in Script UI

1 messages · Page 1 of 1 (latest)

maiden veldt
#
import { CommandPermissionLevel, CustomCommandParamType, system, world } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";

system.beforeEvents.startup.subscribe(({ customCommandRegistry }) => {
    // Register the enum
    customCommandRegistry.registerEnum("npc:emoteChoices", ["shy"]);

    // Register the custom command
    customCommandRegistry.registerCommand(
        {
            name: "npc:emote",
            description: "Plays a custom emote.",
            permissionLevel: CommandPermissionLevel.Any,
            mandatoryParameters: [{ name: "npc:emoteChoices", type: CustomCommandParamType.Enum }]
        },
        ({sourceEntity}, emoteString) => {
            if (emoteString === "shy"){
                system.run(() => { sourceEntity.playAnimation('animation.player.npc.shy'); })
            }
        }
    );
});

world.afterEvents.playerInteractWithEntity.subscribe(({player, target, itemStack}) => {
    if(target?.typeId === 'npc:humanoid' && itemStack?.typeId === 'npc:configure_npc'){
        let npcName = target?.nameTag === undefined ? 'NPC' : target?.nameTag;
        let npcConfigForm = new ModalFormData();
        let npcActionChoices = ['idle', 'sneak', 'sit', 'swim'];
        npcConfigForm.title('NPC Configuration Settings');
        npcConfigForm.textField('NPC Name', 'Insert name here');
        npcConfigForm.dropdown('NPC Action', npcActionChoices);
        npcConfigForm.show(player).then((result) => {
            if(result.canceled) return;
            let [ textField, dropdown ] = result.formValues;
            if(textField){
                target.nameTag = textField;
            }
        }).catch(error => { console.log(error, error.stack)})
    }
});```

I tried using the default value method such as `npcConfigForm.textField('NPC Name', 'Insert name here', target?.nameTag)` but that just emitted an error
maiden veldt
oblique trout
#

line 30: should be

maiden veldt
maiden veldt
#

works