#target.name

1 messages · Page 1 of 1 (latest)

stone storm
#

Cannot find name 'target'.
modal.body(§8${target.name}'s )

import * as ui from "@minecraft/server-ui";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui"
import { MessageFormData } from "@minecraft/server-ui"

//Form Creation
let modal = new ActionFormData()
modal.title("          WHITECUBES-UI")
modal.body(`§8${target.name}'s`) 
//Starts at 0
modal.button("-= Warps =-", "textures/ui_icons/clock_icon")
modal.button("-= Shop =-", "textures/ui_icons/ender_icon")
modal.button("-= Money transfer =-", 'textures/ui_icons/sword_icon')
modal.button("-= Settings =-", "textures/ui_icons/spark_icon")```
#

Is there a reason to why this rejecting? I've seen this used in another form but it wont work in this one

warm turtle
#

You don't have target variable

stone storm
#
export function getScore(target, value, useZero = true) {
    try {
        const objective = world.scoreboard.getObjective(value);
        if (typeof target == "string") return objective.getScore(objective.getParticipants().find((player) => player.displayName == target));
        return objective.getScore(target.scoreboard);
    } catch {
        return useZero ? 0 : NaN;
    }
}``` ?
warm turtle
#

Well, you didn't put the code inside function

#

The target is only for that function

#

And the form didn't on that function

stone storm
#

Could I have a example please?

#

Im really not sure how to fix this, im still learning

warm turtle
#

You need to know what global scope and function scope

stone storm
#

player.displayName = target ?

warm turtle
#
// Global Scope
// Can be used anywhere
const msg = "Hello world!"

function sayHi(target) {
  // Function scope
  const name = target.name; //  Works because target is function scope

  console.log(`${msg} ${name}`;
  // "msg" can be use because it's global
}

const name = target.name
// ERROR: target not found
// That because "target" can be access inside function only (function scope only)
stone storm
#
  // Function scope
  const player.displayName = target.name; ```
#

So something like that?

warm turtle
#

No

#

Leave the function like that

#

You should wrap the form inside function

#
function form(target) {
  //Form Creation
  let modal = new ActionFormData()
  modal.title("          WHITECUBES-UI")
  modal.body(`§8${target.name}'s`) 
  //Starts at 0
  modal.button("-= Warps =-", "textures/ui_icons/clock_icon")
  modal.button("-= Shop =-", "textures/ui_icons/ender_icon")
  modal.button("-= Money transfer =-", 'textures/ui_icons/sword_icon')
  modal.button("-= Settings =-", "textures/ui_icons/spark_icon")
}
stone storm
#
import { world } from "@minecraft/server";
import * as ui from "@minecraft/server-ui";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui"
import { MessageFormData } from "@minecraft/server-ui"
//Form Creation
function form(target) {
  //Form Creation
  let modal = new ActionFormData()
  modal.title("          WHITECUBES-UI")
  modal.body(`§8${target.name}'s`) 
  //Starts at 0
  modal.button("-= Warps =-", "textures/ui_icons/clock_icon")
  modal.button("-= Shop =-", "textures/ui_icons/ender_icon")
  modal.button("-= Money transfer =-", 'textures/ui_icons/sword_icon')
  modal.button("-= Settings =-", "textures/ui_icons/spark_icon")
}

let form2 = new ui.ActionFormData()
form2.title("Tutorial")
form2.body("Hi")
form2.button("Back")

function page1(player) {
    modal.show(player).then(result => {
        let selection = result.selection
        if (selection == 0) {
            player.runCommand(
                "gamemode c @s"
            )
        }
        if (selection == 1) {
            player.runCommand(
                "gamemode s @s"
            )
        }
        if (selection == 2) {
            player.dimension.createExplosion(player.location, 4, {})
        }
        if (selection == 3) {
            let enoughMoney = player.runCommand(`scoreboard players test @s values 50 50`)
            if (enoughMoney) {
                player.runCommand("give @s diamond 1")
                player.runCommand("scoreboard players remove @s values 50")
            }
        }
        if (selection == 4) {
            page2(player)
        }
    })
}

function page2(player) {
    form2.show(player).then(result => {
        let selection = result.selection
        if (selection == 0) {
            page1(player)
        }
    })
}

world.events.beforeItemUse.subscribe(eventData => {
    let player = eventData.source
    let item = eventData.item

    if (item.typeId == "minecraft:compass") {
        page1(player)
    }
})```
warm turtle
#

Yes

#

You can build the form without function, but if you need player name or something from the player, you should wrap inside function and add player or target as argument/parameter

stone storm
#

to add that, would I add what you wrote inside the function ``` // Function scope
const name = target.name;

console.log(`${msg} ${name}```

warm turtle
#

No, that just example

stone storm
#

Because of those reasons

warm turtle
#

Yes, also it's much cleaner if you wrap in a function

stone storm
#

Makes sense, I'll start doing that from now on

warm turtle
#

Oh wait

stone storm
warm turtle
#

Just add target and manually use target.name

stone storm
#

Like I mean, what would it look like?

warm turtle
#
function example(target) {
  // Think of "target" is a player class
  const name = target.name
}
stone storm
#

Thank you

warm turtle
#

Btw, I've seen you has page1 function to show modal, put the modal inside page1 instead of form function

#
function page1(player) {
    //Form Creation
    let modal = new ActionFormData()
    modal.title("          WHITECUBES-UI")
    modal.body(`§8${target.name}'s`) 
    //Starts at 0
    modal.button("-= Warps =-", "textures/ui_icons/clock_icon")
    modal.button("-= Shop =-", "textures/ui_icons/ender_icon")
    modal.button("-= Money transfer =-", 'textures/ui_icons/sword_icon')
    modal.button("-= Settings =-", "textures/ui_icons/spark_icon")

    modal.show(player).then(result => {
        let selection = result.selection
        if (selection == 0) {
            player.runCommand(
                "gamemode c @s"
            )
        }
        if (selection == 1) {
            player.runCommand(
                "gamemode s @s"
            )
        }
        if (selection == 2) {
            player.dimension.createExplosion(player.location, 4, {})
        }
        if (selection == 3) {
            let enoughMoney = player.runCommand(`scoreboard players test @s values 50 50`)
            if (enoughMoney) {
                player.runCommand("give @s diamond 1")
                player.runCommand("scoreboard players remove @s values 50")
            }
        }
        if (selection == 4) {
            page2(player)
        }
    })
}
stone storm
warm turtle
#

That's example, you basically doesn't need it

stone storm
#

So all I need is the compents of that not the function?

warm turtle
#

Example of creating a form

function openForm(player) {
  // Create form here
  const form = new ModalFormData()
  // If you want to display player's name, add `player.name`
  form.title(`${player.name} Statistics`)
  // More form structure

  // The "player" used for showing the form
  form.show(player).then((result) => {
    // Something here...
  })
}
stone storm
#

Sorry if im a pain man, im still learning

warm turtle
#

No worries

stone storm
#
import { world } from "@minecraft/server";
import * as ui from "@minecraft/server-ui";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui"
import { MessageFormData } from "@minecraft/server-ui"

//Form Creation
function Form(player) {
    //Form Creation

    let modal = new ActionFormData()
    modal.title("          WHITECUBES-UI")
    modal.body(`§8${player.name}'s`)-
    //Starts at 0
    modal.button("-= Warps =-", "textures/ui_icons/clock_icon")
    modal.button("-= Shop =-", "textures/ui_icons/ender_icon")
    modal.button("-= Money transfer =-", 'textures/ui_icons/sword_icon')
    modal.button("-= Settings =-", "textures/ui_icons/spark_icon")
}``` So in the end should it look like this?
warm turtle
#

Yes

stone storm
#

It's still saying target can not be found

#
import { world } from "@minecraft/server";
import * as ui from "@minecraft/server-ui";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui"
import { MessageFormData } from "@minecraft/server-ui"

//Form Creation
function Form(player) {
    //Form Creation

    let modal = new ActionFormData()
    modal.title("          WHITECUBES-UI")
    modal.body(`§8${player.name}'s`)
        //Starts at 0
        modal.button("-= Warps =-", "textures/ui_icons/clock_icon")
        modal.button("-= Shop =-", "textures/ui_icons/ender_icon")
        modal.button("-= Money transfer =-", 'textures/ui_icons/sword_icon')
        modal.button("-= Settings =-", "textures/ui_icons/spark_icon")
}

let form2 = new ui.ActionFormData()
form2.title("Tutorial")
form2.body("Hi")
form2.button("Back")

function page2(player) {
    //Form Creation
    let modal = new ActionFormData()
    modal.title("          WHITECUBES-UI")
    modal.body(`§8${player.name}'s`)

    //Starts at 0
    modal.button("-= Warps =-", "textures/ui_icons/clock_icon")
    modal.button("-= Shop =-", "textures/ui_icons/ender_icon")
    modal.button("-= Money transfer =-", 'textures/ui_icons/sword_icon')
    modal.button("-= Settings =-", "textures/ui_icons/spark_icon")

    modal.show(player).then(result => {
        let selection = result.selection
        if (selection == 0) {
            player.runCommand(
                "gamemode c @s"
            )
        }
        if (selection == 1) {
            player.runCommand(
                "gamemode s @s"
            )
        }
        if (selection == 2) {
            player.dimension.createExplosion(player.location, 4, {})
        }
        if (selection == 3) {
            let enoughMoney = player.runCommand(`scoreboard players test @s values 50 50`)
            if (enoughMoney) {
                player.runCommand("give @s diamond 1")
                player.runCommand("scoreboard players remove @s values 50")
            }
        }
        if (selection == 4) {
            page2(player)
        }
    })
}```
reef valeBOT
#
Debug Result

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

<repl>.js:29:21 - error TS2304: Cannot find name 'target'.

29     modal.body(`§8${target.name}'s`)
                       ~~~~~~

warm turtle
#

You use target.name while there's player

#

Change it to player.name

reef valeBOT
#
No Errors

No errors in [code](#1116022078420824064 message)

stone storm
#
import { world } from "@minecraft/server";
import * as ui from "@minecraft/server-ui";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui"
import { MessageFormData } from "@minecraft/server-ui"

//Form Creation
function Form(player) {
    //Form Creation



    let modal = new ActionFormData()
    modal.title("          WHITECUBES-UI")
    modal.body(`§8${player.name}'s`)
    //Starts at 0
    modal.button("-= Warps =-", "textures/ui_icons/clock_icon")
    modal.button("-= Shop =-", "textures/ui_icons/ender_icon")
    modal.button("-= Money transfer =-", 'textures/ui_icons/sword_icon')
    modal.button("-= Settings =-", "textures/ui_icons/spark_icon")
}

let form2 = new ui.ActionFormData()
form2.title("Tutorial")
form2.body("Hi")
form2.button("Back")

function page2(player) {
    //Form Creation
    let modal = new ActionFormData()
    modal.title("          WHITECUBES-UI")
    modal.body(`§8${player.name}'s`)

    //Starts at 0
    modal.button("-= Warps =-", "textures/ui_icons/clock_icon")
    modal.button("-= Shop =-", "textures/ui_icons/ender_icon")
    modal.button("-= Money transfer =-", 'textures/ui_icons/sword_icon')
    modal.button("-= Settings =-", "textures/ui_icons/spark_icon")

    modal.show(player).then(result => {
        let selection = result.selection
        if (selection == 0) {
            player.runCommand(
                "gamemode c @s"
            )
        }
        if (selection == 1) {
            player.runCommand(
                "gamemode s @s"
            )
        }
        if (selection == 2) {
            player.dimension.createExplosion(player.location, 4, {})
        }
        if (selection == 3) {
            let enoughMoney = player.runCommand(`scoreboard players test @s values 50 50`)
            if (enoughMoney) {
                player.runCommand("give @s diamond 1")
                player.runCommand("scoreboard players remove @s values 50")
            }
        }
        if (selection == 4) {
            page2(player)

        }
    })
}

function page2(player) {
    form2.show(player).then(result => {
        let selection = result.selection
        if (selection == 0) {
            page2(player)
        }
    })
}

world.events.beforeItemUse.subscribe(eventData => {
    let player = eventData.source
    let item = eventData.item

    if (item.typeId == "minecraft:compass") {
        page2(player)
    }
})
reef valeBOT
#
Debug Result

There are 2 errors in this [code](#1116022078420824064 message):

<repl>.js:72:13 - error TS2552: Cannot find name 'page1'. Did you mean 'page2'?

72             page1(player)
               ~~~~~

  <repl>.js:27:10
    27 function page2(player) {
                ~~~~~
    'page2' is declared here.

``````ansi
<repl>.js:82:9 - error TS2552: Cannot find name 'page1'. Did you mean 'page2'?

82         page1(player)
           ~~~~~

  <repl>.js:27:10
    27 function page2(player) {
                ~~~~~
    'page2' is declared here.

#
Debug Result

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

<repl>.js:72:13 - error TS2552: Cannot find name 'page1'. Did you mean 'page2'?

72             page1(player)
               ~~~~~

  <repl>.js:27:10
    27 function page2(player) {
                ~~~~~
    'page2' is declared here.

#
No Errors

No errors in [code](#1116022078420824064 message)

warm turtle
#

Be careful, you have 2 page2 function

stone storm
#

I've got a headache honestly

stone storm
warm turtle
#

I think you need to rename one of those page2 into page1

stone storm
#
function page1(player) {
    form2.show(player).then(result => {
        let selection = result.selection
        if (selection == 0) {
            page2(player)
        }
    })
}

world.events.beforeItemUse.subscribe(eventData => {
    let player = eventData.source
    let item = eventData.item

    if (item.typeId == "minecraft:compass") {
        page1(player)
    }
})```
warm turtle
#

Yes? Try it

stone storm
#

Works but it doesnt open the main gui it opens the second one

#

would it be modal?

warm turtle
#

You call the page1 but inside page1, there's form2

#

What I think you need to open is page2 because there's modal form in it

stone storm
#
    modal.show(player).then(result => {
        let selection = result.selection
        if (selection == 0) {
            page1(player)
        }
    })
}```?
warm turtle
#

Nooo, you already has page2

#

Please check your code again

stoic mauve
stone storm
#

OK I worked it out

stone storm
stone storm
stoic mauve
# stone storm Thank you but its fixed now

but are you in version 1.19.80?because they released the stable update 1.20,that the parameter "world.events.beforeItemUse.subscribe" has been updated to "world.beforeEvents.itemUse.subscribe"

stone storm
#

Why do they always change it?