ScoreSystem bug!

You see when I first didn’t it was like 00 disappeared but then I realized I just had to resize it.

And for what’s next I think I need to add the numbers and call them as events in the same script

Great, you are ready! No need to add any numbers, just start calling that event, like this from any script to increment the score:

this.app.fire('player:scored');

If you are curious you can try that directly from the browser console, by using a slightly varied method, check my screen grab below:

pc.app.fire('player:scored');

So you telling me I don’t need a score element,but how would the score change,or is it automated by the script

You already have a score element, that is what we are seeing on screen.

The score changes each time you call this:

this.app.fire('player:scored');

It will invoke the event listener on your Scorep.js script. In there we increment the this.score variable at line 11 and update your Score entity to render it on screen on line 12.

That’s it, in the same manner you can easily create an event to reset the score, or set it to any value you like.

So if I already have everything why isn’t the score changing unless I have to do

this.app.fire('player:scored);
this.app.fire('player:scored);
this.app.fire('player:scored);

That’s the developers job!

This code won’t work automagically, you need to put that event listener that you proudly created at the right place in your game code. When the players scores in your game, at that instant you need to call that line of code.

I believe in you :wink:

Alright I’ll see what I can do

Ok so I’m still confused I have put the score in the right place in my game code


But the code stilll doesn’t work
this the code I used

var Scorep = pc.createScript('scorep');



var score = "game";
console.log('Score started');

Scorep.prototype.initialize = function(){
   this.score = 0;
    this.app.fire('player:scored', function(){
        this.app.fire('01:scored');
        this.app.fire('02:scored');
      this.score++;
      this.entity.element.text = this.score;
   }, this);
};

So, why did you change this line:

this.app.on('player:scored', function(){

To this:

this.app.fire('player:scored', function(){

???

You basically removed your event listener, so no matter where you call the fire method there isn’t anyone listening.

Next you call an event that doesn’t exist, inside your event listener:

this.app.fire('01:scored');

There isn’t any event named like that. @Marquise_Edwards I am afraid I can’t help you more here. You need to study closely the Playcanvas events system, experiment and learn it. Much like you did with the blank project, take it slow and start building your knowledge bit by bit.

Start here: https://developer.playcanvas.com/en/user-manual/scripting/communication/

Ok So I’m confused about how I’m supposed to make this script change the number’s

var Scorep = pc.createScript('scorep');



var score = "game";
console.log('Score started');

Scorep.prototype.initialize = function(){
   this.score = 0;
    this.app.on('player:score', function(){
      this.score++;
      this.entity.element.text = this.score;
   }, this);
};

@Marquise_Edwards do avoid opening new posts for the same subject, it makes it difficult for people to follow and help you out.

https://forum.playcanvas.com/t/scoresystem-bug/11453/32

Try changing this.score++; to this.score += 1;

// this.score++;
   this.score += 1;

Yes I know.
But the thing is the numbers are not showing up in the game.
Here’s the editor link:https://playcanvas.com/editor/scene/773725
And here’s the code link:https://playcanvas.com/editor/code/624824?tabs=25529770,21805247

Ok so I’ve been trying to figure out why the score keeps repeating after the result45%20PM
Heres the game link:https://playcanv.as/p/oY6NfOfU/
Here’s the code:



ScoreSystem.prototype.intalize = function() {
  this(function(reset) {
      if(this.app.keyboard.isPressed(pc.KEY_R)) {
          this.entity.findByName('Ball',this.reset);
          this.entity.resetCurrentPosition('0');
          this.entity = true;
      }
      
  });
};
//Player
ScoreSystem.attributes.add("player", { type: 'entity', title: 'Player' });

//Variables
var score = 0;

// initialize code called once per entity
ScoreSystem.prototype.initialize = function() {
    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
    this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp, this);
    this.zero.enabled = true;
    this.one.enabled = false;
    this.two.enabled = false;
    this.three.enabled = false;
    this.four.enabled = false;
    this.five.enabled = false;
    this.six.enabled = false;
    this.seven.enabled = false;
    this.eight.enabled = false;
    this.nine.enabled = false;
    this.ten.enabled = false;
   
};

// update code called every frame
ScoreSystem.prototype.update = function(dt) {
    //System
    if (this.player.getPosition().z <= this.p1.getPosition().z && this.player.getPosition().z > this.p2.getPosition().z) {
        score = 1;
    }     
    if (this.player.getPosition().z <= this.p2.getPosition().z && this.player.getPosition().z > this.p3.getPosition().z) {
        score = 2;
    }
    if (this.player.getPosition().z <= this.p3.getPosition().z && this.player.getPosition().z > this.p4.getPosition().z) {
        score = 3;
    }
    if (this.player.getPosition().z <= this.p4.getPosition().z && this.player.getPosition().z > this.p5.getPosition().z) {
        score = 4;
    }
    if (this.player.getPosition().z <= this.p5.getPosition().z && this.player.getPosition().z > this.p6.getPosition().z) {
        score = 5;    
    }
    if (this.player.getPosition().z <= this.p6.getPosition().z && this.player.getPosition().z > this.p7.getPosition().z) {
        score = 6;
    }
    if (this.player.getPosition().z <= this.p7.getPosition().z && this.player.getPosition().z > this.p8.getPosition().z) {
        score = 7;
    }
    if (this.player.getPosition().z <= this.p8.getPosition().z && this.player.getPosition().z > this.p9.getPosition().z) {
        score = 8;
    }
    if (this.player.getPosition().z <= this.p9.getPosition().z && this.player.getPosition().z > this.p10.getPosition().z) {
        score = 9;
    }
    if (this.player.getPosition().z <= this.p10.getPosition().z && this.player.getPosition().z > this.p10.getPosition().z) {
        score = 10;
    }

     
    
    
    
    if (score === 1) {
        this.zero.enabled = false;
        this.one.enabled = false;
        this.two.enabled = false;
    }
    if (score === 2) {
        this.one.enabled = false;
        this.two.enabled = true;
        this.three.enabled = false;
    }
    if (score === 3) {
        this.two.enabled = false;
        this.three.enabled = true;
        this.four.enabled = false;
    }
    if (score === 4) {
        this.three.enabled = false;
        this.four.enabled = true;
        this.five.enabled = false;
    }
    if (score === 5) {
        this.four.enabled = false;
        this.five.enabled = true;
        this.six.enabled = false;
    }
    if (score === 6) {
        this.five.enabled = false;
        this.six.enabled = true;
        this.seven.enabled = false;
    }
    if (score === 7) {
        this.six.enabled = false;
        this.seven.enabled = true;
        this.eight.enabled = false;
    }
    if (score === 8) {
        this.seven.enabled = false;
        this.eight.enabled = true;
        this.nine.enabled = false;
    }
    if (score === 9) {
        this.eight.enabled = false;
        this.nine.enabled = true;
        this.ten.enabled = false;
    }
    if (score === 10) {
        this.nine.enabled = false;
        this.ten.enabled = true;
        
    }
  
};

// swap method called for script hot-reloading
// inherit your script state here
// ScoreSystem.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/


Welp @Leonidas I wanna thank you,thnx to you I was able to find the problem and solve .
Turns out the 01 was the issue when you reset the number the 01 glitches the system and makes it think that it’s supposed to reset with 01 and the rest of them numbers which bugged it out.SO I took 01 out now it starts at 02 and works fine.I’ll continue to work on the system till I’m able to add 01 back in without glitching

1 Like