# Multiplayer damage script

**URL:** <https://forum.playcanvas.com/t/multiplayer-damage-script/22298>\
**Category:** Help & Support\
**Created:** [October 6, 2021, 3:50pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298 "2021-10-06T15:50:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![quantum](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/quantum/32/17694_2.png) [@quantum](https://forum.playcanvas.com/u/quantum)\
**Post date:** [October 6, 2021, 3:50pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/1 "2021-10-06T15:50:43Z")

</div>

i am making a FPS multiplayer game. i don’t know how to make one player take damage when a different player shoots at them. could someone please help?  
server i am using: nodejs socket server.

---

<div class="post-metadata">

**Author:** ![Gavin\_Durbin](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/gavin_durbin/32/12035_2.png) [@Gavin\_Durbin](https://forum.playcanvas.com/u/Gavin_Durbin)\
**Post date:** [October 6, 2021, 4:44pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/2 "2021-10-06T16:44:52Z")

</div>

Are you using raycast or physical shooting

---

<div class="post-metadata">

**Author:** ![quantum](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/quantum/32/17694_2.png) [@quantum](https://forum.playcanvas.com/u/quantum)\
**Post date:** [October 6, 2021, 5:04pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/3 "2021-10-06T17:04:32Z")

</div>

raycast i think. it uses a script, not a physical entity.

---

<div class="post-metadata">

**Author:** ![Gavin\_Durbin](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/gavin_durbin/32/12035_2.png) [@Gavin\_Durbin](https://forum.playcanvas.com/u/Gavin_Durbin)\
**Post date:** [October 6, 2021, 5:44pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/4 "2021-10-06T17:44:54Z")

</div>

ok, then here

```javascript
if(result.entity.name == 'other player')
        {
            result.entity.script.enemy.TakeDamage(this.damage);
        }

```

---

<div class="post-metadata">

**Author:** ![quantum](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/quantum/32/17694_2.png) [@quantum](https://forum.playcanvas.com/u/quantum)\
**Post date:** [October 6, 2021, 5:45pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/5 "2021-10-06T17:45:53Z")

</div>

thanks i will try it. i put this in the shooting script right?

---

<div class="post-metadata">

**Author:** ![quantum](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/quantum/32/17694_2.png) [@quantum](https://forum.playcanvas.com/u/quantum)\
**Post date:** [October 6, 2021, 5:56pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/6 "2021-10-06T17:56:01Z")

</div>

its not working.  
here is my shooting script:

```javascript
var Shoot = pc.createScript('shoot');

Shoot.attributes.add('maxAmmo', {type : 'number', default : 30});
Shoot.attributes.add('range', {type : 'number', default : 30});
Shoot.attributes.add('text', {type : 'entity'});
Shoot.attributes.add('text2', {type : 'entity'});
Shoot.attributes.add('particleSys', {type : 'entity'});
Shoot.attributes.add('debugColor1', {type : 'rgba'});
Shoot.attributes.add('gunPoint', {type : 'entity'});
Shoot.attributes.add('hitImpulse', {type : 'number', default : 10});
Shoot.attributes.add('damage', {type : 'number', default : 10});
Shoot.attributes.add('camera', {type : 'entity'});

// initialize code called once per entity
Shoot.prototype.initialize = function() {
    this.currentAmmo = 0;
    this.currentAmmo = this.maxAmmo;
    this.canShoot = true;
    
};

// update code called every frame
Shoot.prototype.update = function(dt) {
    var from = this.gunPoint.getPosition();
    var to = this.camera.camera.screenToWorld(screen.width / 2 , screen.height / 2 - 37, this.range);
    var result = this.app.systems.rigidbody.raycastFirst(from, to);    
    
    
    if(this.currentAmmo === 0)
    {
        this.canShoot = false;
    }
    else
    {
        this.canShoot = true;
    }
    
    
    if(this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT) && this.canShoot === true)
    {
        if(result)
        {
            result.entity.collision.entity.rigidbody.applyImpulse(this.camera.forward.scale(this.hitImpulse), result.normal);
            this.currentAmmo -= 1;
            
        }else
        {
            return;
        }
        
        if(result.entity.name == 'Other')
        {
            result.entity.script.enemy.TakeDamage(this.damage);
        }

        this.particleSys.particlesystem.play();
        this.particleSys.particlesystem.reset();
        
    }
    
    
    
    if(this.app.keyboard.wasPressed(pc.KEY_R))
    {
        
        this.currentAmmo = this.currentAmmo + (this.maxAmmo - this.currentAmmo);
        
         
    }
    
    this.text.element.text = this.currentAmmo;
    this.text2.element.text = '/ ' + this.maxAmmo;
    
};

```

---

<div class="post-metadata">

**Author:** ![Gavin\_Durbin](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/gavin_durbin/32/12035_2.png) [@Gavin\_Durbin](https://forum.playcanvas.com/u/Gavin_Durbin)\
**Post date:** [October 6, 2021, 6:01pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/7 "2021-10-06T18:01:39Z")

</div>

is this from @wasd1234?

---

<div class="post-metadata">

**Author:** ![Gavin\_Durbin](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/gavin_durbin/32/12035_2.png) [@Gavin\_Durbin](https://forum.playcanvas.com/u/Gavin_Durbin)\
**Post date:** [October 6, 2021, 6:02pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/8 "2021-10-06T18:02:10Z")

</div>

if this is i think i may know it well enough

---

<div class="post-metadata">

**Author:** ![quantum](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/quantum/32/17694_2.png) [@quantum](https://forum.playcanvas.com/u/quantum)\
**Post date:** [October 6, 2021, 6:02pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/9 "2021-10-06T18:02:48Z")

</div>

no it is from Quantumgames from project Left 2 Die  
[https://playcanvas.com/project/770449/overview/battle-legends-season-0](https://playcanvas.com/project/770449/overview/battle-legends-season-0)

---

<div class="post-metadata">

**Author:** ![quantum](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/quantum/32/17694_2.png) [@quantum](https://forum.playcanvas.com/u/quantum)\
**Post date:** [October 6, 2021, 6:03pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/10 "2021-10-06T18:03:41Z")

</div>

i did get the script from that person though yes

---

<div class="post-metadata">

**Author:** ![Albertos](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/albertos/32/19749_2.png) [@Albertos](https://forum.playcanvas.com/u/Albertos)\
**Post date:** [October 6, 2021, 6:04pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/11 "2021-10-06T18:04:55Z")

</div>

The code from @Gavin_Durbin should be inside your _’if result’ statement_ otherwise you get an error.

---

<div class="post-metadata">

**Author:** ![quantum](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/quantum/32/17694_2.png) [@quantum](https://forum.playcanvas.com/u/quantum)\
**Post date:** [October 6, 2021, 6:06pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/12 "2021-10-06T18:06:08Z")

</div>

ok thanks i will try it

---

<div class="post-metadata">

**Author:** ![Gavin\_Durbin](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/gavin_durbin/32/12035_2.png) [@Gavin\_Durbin](https://forum.playcanvas.com/u/Gavin_Durbin)\
**Post date:** [October 6, 2021, 6:06pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/13 "2021-10-06T18:06:57Z")

</div>

he already has the part i told him about in there already, the code right there from wasd1234 already had the code i told him about in there, unless he deleted it, so he jusy needs to change the script names and collision entity name

---

<div class="post-metadata">

**Author:** ![quantum](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/quantum/32/17694_2.png) [@quantum](https://forum.playcanvas.com/u/quantum)\
**Post date:** [October 6, 2021, 6:07pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/14 "2021-10-06T18:07:09Z")

</div>

it is in the if result but it won’t work. it does not give an error, but it does not take away health from the other player

---

<div class="post-metadata">

**Author:** ![Albertos](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/albertos/32/19749_2.png) [@Albertos](https://forum.playcanvas.com/u/Albertos)\
**Post date:** [October 6, 2021, 6:08pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/15 "2021-10-06T18:08:20Z")

</div>

Has the entity the name ‘Other’?

---

<div class="post-metadata">

**Author:** ![quantum](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/quantum/32/17694_2.png) [@quantum](https://forum.playcanvas.com/u/quantum)\
**Post date:** [October 6, 2021, 6:09pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/16 "2021-10-06T18:09:59Z")

</div>

that is the multiplayer placeholder entity yes

---

<div class="post-metadata">

**Author:** ![Albertos](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/albertos/32/19749_2.png) [@Albertos](https://forum.playcanvas.com/u/Albertos)\
**Post date:** [October 6, 2021, 6:18pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/17 "2021-10-06T18:18:38Z")

</div>

Placeholder? If you change the name by script after initialize it won’t work.

---

<div class="post-metadata">

**Author:** ![quantum](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/quantum/32/17694_2.png) [@quantum](https://forum.playcanvas.com/u/quantum)\
**Post date:** [October 6, 2021, 6:20pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/18 "2021-10-06T18:20:05Z")

</div>

no what i mean is this:  
the multiplayer server is from the real time multiplayer playcanvas tutorial. i don’t know exactly what “other” is, but i think it is the other player from multiplayer.

---

<div class="post-metadata">

**Author:** ![quantum](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/quantum/32/17694_2.png) [@quantum](https://forum.playcanvas.com/u/quantum)\
**Post date:** [October 6, 2021, 6:21pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/19 "2021-10-06T18:21:35Z")

</div>

my new script is this

```javascript
var RaycastShooting = pc.createScript('raycastShooting');

RaycastShooting.attributes.add('Ammo', {type : 'number', default : 30});
RaycastShooting.attributes.add('range', {type : 'number', default : 30});
RaycastShooting.attributes.add('text', {type : 'entity'});
RaycastShooting.attributes.add('debugColor1', {type : 'rgba'});
RaycastShooting.attributes.add('gunPoint', {type : 'entity'});
RaycastShooting.attributes.add('hitImpulse', {type : 'number', default : 10});
RaycastShooting.attributes.add('camera', {type : 'entity'});

// initialize code called once per entity
RaycastShooting.prototype.initialize = function() {
    this.currentAmmo = 0;
    this.currentAmmo = this.Ammo;
    
    
};

// update code called every frame
RaycastShooting.prototype.update = function(dt) {
    var canShoot = true;
    var from = this.gunPoint.getPosition();
    var to = this.camera.camera.screenToWorld(screen.width / 2 , screen.height / 2 - 37, this.range);
    var result = this.app.systems.rigidbody.raycastFirst(from, to);
    var nextFire = 0;
    
    
    
    if(this.currentAmmo === 0)
    {
        canShoot = false;
    }
    else
    {
        canShoot = true;
    }
    
    
    if(this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT) && canShoot === true)
    {
        if(result)
        {
            result.entity.collision.entity.rigidbody.applyImpulse(result.normal.scale(-this.hitImpulse));
        }
        this.currentAmmo -= 1;
        
        this.app.renderLine(from, to, this.debugColor1);
    }
    
    if(result.entity.name == 'Other')
        {
            result.entity.script.Other.TakeDamage(this.damage);
        }
    
    if(this.currentAmmo < this.Ammo && this.app.keyboard.wasPressed(pc.KEY_R))
    {
        
        this.currentAmmo += (this.Ammo - this.currentAmmo);
        
         
    }

```

it results as an error:

```javascript
[raycastshoot.js?id=56781704&branchId=0ca9042a-e9f0-45cb-9b34-dfcbe75a537f:57]: Cannot read property 'enemy' of undefined

TypeError: Cannot read property 'enemy' of undefined
    at scriptType.RaycastShooting.update (https://launch.playcanvas.com/api/assets/files/Scripts/gun%20scripts/raycastshoot.js?id=56781704&branchId=0ca9042a-e9f0-45cb-9b34-dfcbe75a537f:57:34)

```

---

<div class="post-metadata">

**Author:** ![Albertos](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/albertos/32/19749_2.png) [@Albertos](https://forum.playcanvas.com/u/Albertos)\
**Post date:** [October 6, 2021, 6:23pm UTC](https://forum.playcanvas.com/t/multiplayer-damage-script/22298/20 "2021-10-06T18:23:35Z")

</div>

My point is that _other_ also is a _player_ that use the same script I guess.

[Next page](https://forum.playcanvas.com/t/multiplayer-damage-script/22298.md?page=2)
