Multiplayer damage script

ok i will try to add the shooting script and the health script to “other”
i did, now i get this error:

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

TypeError: Cannot read property 'TakeDamage' 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:40)
    at ScriptComponent._scriptMethod (https://code.playcanvas.com/playcanvas-stable.dbg.js:63658:19)
    at ScriptComponent._onUpdate (https://code.playcanvas.com/playcanvas-stable.dbg.js:63706:11)
    at ScriptComponentSystem._callComponentMethod (https://code.playcanvas.com/playcanvas-stable.dbg.js:64272:49)
    at ScriptComponentSystem._onUpdate (https://code.playcanvas.com/playcanvas-stable.dbg.js:64289:9)
    at Function._helper (https://code.playcanvas.com/playcanvas-stable.dbg.js:43417:12)
    at Function.update (https://code.playcanvas.com/playcanvas-stable.dbg.js:43432:9)
    at Application.update (https://code.playcanvas.com/playcanvas-stable.dbg.js:70790:20)
    at https://code.playcanvas.com/playcanvas-stable.dbg.js:71493:16

As far as I know you just need to add all scripts to the player entity. The player will also be an enemy and vice versa because it is a multiplayer game.

ok i will try thanks

it is still not working, so i don’t know what to do. i am getting the same TakeDamage error as before…

You first have to understand the logic. I will give it a try tomorrow.

I’ve looked at your project and see some mistakes.

Your takeDamage line has to be inside the result statement as I mentioned earlier. Right now it’s also executed when the player is not shooting and thats not what you want.

After result.entity.script need to be the script name. Right now it’s looking for the script ‘Other’, which not exist. Probably you want to use result.entity.script.health, because that script is on your Other entity. After the script name need to be the function inside that script. The TakeDamage function exist only in your target script, but you don’t use that script on your Other entity, so you can’t reach this function. Based on your health script you can use the damageHP function. The complete line will look like below.

result.entity.script.health.damageHP(this.damage);

Last thing is that you use this.damage, but I can’t see where you define it. Make sure you define it somewhere. For example in your initialize function.

this.damage = 10;

thanks so much for your help! i will try it

oh k… now i am getting a different error.

Error loading scripts. Open the browser console for details.

Error loading scripts. Open the browser console for details.

Error loading scripts. Open the browser console for details.

Error loading scripts. Open the browser console for details.

Error loading scripts. Open the browser console for details.

and the preview won’t even load…

oh its because i have stuff wrong with the scripting. i am going to switch back to my old shooting script but with the new functions…

ok so now that i added what you told me to add, it made a clone of the player without me launching the game twice, and if you shoot it, it harms yourself. same thing if i launch the game twice, it just takes away the player’s health that is shooting…

So the game is multiplayer, and when you shoot the other player it damages you? Just you or the other too?

just the player who is shooting

To be sure, do you use the Other entity or the enemy entity and what is the difference?

image

i use the other entity. the difference is the enemy entity is for singleplayer, and is not connected to the server.

Okay, it’s hard to test your game. Can you tell me which scene I have to use to test the shooting part?

season 3-multiplayer

Can you share a link to the game?

editor link: https://playcanvas.com/editor/scene/1242249

Your other entity use the health attributes of your player entity. Your other entity need it’s own health attributes.

i made a new health script, but it is still doing the same thing… ugh i feel stoopid right now