Ask coding questions

← Back to all posts
Multiplayer help!!!!!!!!!!!
duck132912 (210)

I need help removing the player when the client leaves, I cant figure exactly how to do that yet

Answered by TigerTheCat (45) [earned 5 cycles]
View Answer
Comments
hotnewtop
TigerTheCat (45)

I had this same problem too. You can use

socket.on('disconnect', () => {
   console.log('User left: '+socket.id)
   // removing player stuff here 
})

Hope I helped!

TigerTheCat (45)

@duck132912 you also have to put in the player removing stuff

TigerTheCat (45)

@duck132912 Okay, I figured I out.

socket.on('disconnect', () => {
   console.log('User left: '+socket.id)
   delete players[socket.id] 
})

Please let me know if this works!

duck132912 (210)

@TigerTheCat Thx soo much, ima check if it works

TigerTheCat (45)

@duck132912 No problem! I hope it works!

duck132912 (210)

@TigerTheCat It works!!! I never knew you can do that!

TigerTheCat (45)

@duck132912 I'm glad I could help! When I had this problem, I had to spend 5 hours on Stack Overflow trying to figure out how to detect disconnect