Forum Discussion

LitlMan69's avatar
LitlMan69
Member
8 months ago

Typescripts

Help here, I just created this typescript but it seems not working, any idea how to get it fixed? I would really appreciate any help, thanks.

 sorry for the sideway picture.

6 Replies

  • Your if statement is this,startTime. It should be this.startTime. your CodeBlockEvent might also need a hz. In front of it. There is also a space between this. and connectCodeBlockEvents. It also looks like your console.log is not displaying a string. It should be enclosed in ""

     

    What IDE are you using?

    I'm currently on my phone. If your still having issues when I get back I'll take a look is Visual Studios Community and post it here for you.

    • LitlMan69's avatar
      LitlMan69
      Member

      Ok thanks let me look it and I will let you know. Thanks

  • Bellow I have the code with some notes. I tested my changes it in a blank world and it printed both for me.

    import * as hz from 'horizon/core';
    
    class TimeSpentScriptts extends hz.Component<typeof TimeSpentScriptts> {
        static propsDefinition = {};
    
        private startTime: number = 0;
        start() {
            // make sure there are no empty spaces
            // code is case sensitive
            // double check spelling
            // do not forget the hz if you are importing * as hz.
            // startTime is not declared anywhere. I declared it for you, see below the propsDefinition.
            // double check for accidental , instead of .
            // Dont forget to wrap your strings with "" or ``. Since you are using ${} use ``
            // Cannot assign a number to null
    
            // your code
            // this. ConnectCodeBlockEvent(this.entity, CodeBlockEvent.OnPlayerEnterWorld,(player:player) => { this.startTime = Date.now();
    
            // this is how it should look
            this.connectCodeBlockEvent(this.entity, hz.CodeBlockEvents.OnPlayerEnterWorld, (player: hz.Player) => {
                this.startTime = Date.now();
                console.log("startTime: " + this.startTime);
            });
    
            // your code
            //this.connectCodeBlockEvent(this.entity, CodeBlockEvents.OnPlayerexitWorld, (player: player) => {
                //if(this,startTime !== null) {
                //const timeSpent =(Date.now()
                //- this.startTime) / 1000;
                //console.log(Player ${player.name.get()} spent ${timespent}
                //seconds in the world.);
                //this.startTime = null;
                //}
            //});
    
            // this is how it should look
            this.connectCodeBlockEvent(this.entity, hz.CodeBlockEvents.OnPlayerExitWorld, (player: hz.Player) => {
                if (this.startTime !== null) {
                    const timeSpent = (Date.now() - this.startTime) / 1000;
                    console.log(`Player ${player.name.get()} spent ${timeSpent } seconds in the world.`);
                    this.startTime = 0;
                }
            });
        }
    }
    hz.Component.register(TimeSpentScriptts);

    This is what your code looks like in my IDE (Visual Studio Community). I'm not sure what you are using to write code but if you use Visual Studio Community or even Visual Studio Code it should tell you if there is an error with your code with the red lines. It will go a long way in helping you learn proper syntax and prevent syntax errors/bugs.