#  Cannot read properties of undefined (reading 'setVelocityY'), but object is defined in one of if branches

**URL:** <https://phaser.discourse.group/t/cannot-read-properties-of-undefined-reading-setvelocityy-but-object-is-defined-in-one-of-if-branches/12847>\
**Category:** Phaser 3\
**Created:** [February 4, 2023, 10:04pm UTC](https://phaser.discourse.group/t/cannot-read-properties-of-undefined-reading-setvelocityy-but-object-is-defined-in-one-of-if-branches/12847 "2023-02-04T22:04:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pali08](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/pali08/32/7354_2.png) [@pali08](https://phaser.discourse.group/u/pali08)\
**Post date:** [February 4, 2023, 10:04pm UTC](https://phaser.discourse.group/t/cannot-read-properties-of-undefined-reading-setvelocityy-but-object-is-defined-in-one-of-if-branches/12847/1 "2023-02-04T22:04:34Z")

</div>

Hi all. I am new to game making and also to [phaser.io](http://phaser.io).

I am trying to make multiplayer foosball game using [socket.io](http://socket.io). I am getting information from [this tutorial](https://gamedevacademy.org/create-a-basic-multiplayer-game-in-phaser-3-with-socket-io-part-1/) mostly. Here is my code:  
[GitHub - pali08/foosball\_in\_phaser\_multiplayer](https://github.com/pali08/foosball_in_phaser_multiplayer)

The problem occurs in [game.js](https://github.com/pali08/foosball_in_phaser_multiplayer/blob/main/public/js/game.js) file in function handleKeyboardInput in second else if branch (line 123):

```javascript
else if (cursorsUp.isUp) {
      console.log(typeof(players) == null)
      console.log(typeof(players))
      players.setVelocityY(0);
}

```

I want group of players to move when I press the button, but stop moving when key is released. For some reason I get

```javascript
game.js:126 Uncaught TypeError: Cannot read properties of undefined (reading 'setVelocityY')
    at handleKeyboardInput (game.js:126:15)
    at initialize.update (game.js:67:3)
    at initialize.step (phaser.min.js:1:225442)
    at initialize.update (phaser.min.js:1:490723)
    at initialize.step (phaser.min.js:1:793006)
    at initialize.step (phaser.min.js:1:433503)
    at t (phaser.min.js:1:434427)

```

when executing game. The black background appears without players

The strange thing is, that

- first two (if and else if) branches works correctly - player moves up and down (but does not stop when key is released)
- If I remove content of `else if (cursorsUp.isUp)`, program is executable, but last else if branch:

```javascript
else if (cursorsDown.isUp) {
       players.setVelocityY(0);
   }

```

does not work. When down arrow is released, players doesn’t stop.

Any idea, where problem might be?

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [February 4, 2023, 10:14pm UTC](https://phaser.discourse.group/t/cannot-read-properties-of-undefined-reading-setvelocityy-but-object-is-defined-in-one-of-if-branches/12847/2 "2023-02-04T22:14:36Z")

</div>

```
cursors = this.input.keyboard.createCursorKeys();

```

Move this to scene `create()`, and add

```
let cursors;

```

at the top.

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [February 4, 2023, 10:55pm UTC](https://phaser.discourse.group/t/cannot-read-properties-of-undefined-reading-setvelocityy-but-object-is-defined-in-one-of-if-branches/12847/3 "2023-02-04T22:55:54Z")

</div>

`currentPlayers` is undefined until the `'currentGamer'` event listener is called, but `update()` starts running before that.
