Error with my battle royale game! please help!

when one player in my multiplayer game shoots and killed another player, they get this error:
TypeError: Cannot read property ‘getEulerAngles’ of undefined
at scriptType.LoginRegister.updatePosition

i don’t understand this. can someone please help me fix it so the player actually dies in the game and gets sent back to the login screen?

Hello @quantumgames and welcome! Do you try to get the euler angles of an entity that doesn’t exist anymore (because this player is dead)?

i don’t know. the entity of the player is the same for all players, and i tried deleting that line of code, but more errors popped up. do you know what i could do so that the player gets deleted without an error?

Do you have a link to your project or can you add me to your project?

i have a link: https://playcanvas.com/editor/scene/1117154 see if that works.
the file is loginregister.js

I would try to add something like below first and then see if everthing works as it should be.

if (this.player !== undefined) {
   // your updatePosition code here
}

ok thanks i will try it!

no it still shows up with the same error.

Hi @quantumgames,

Can you please show your full script? (Also better with screenshot of the error)

Then you have to try to find out what is undefined and after that why it is undefined. You can use for example console.log(this.player) to see in the console of your browser if the result is as expected.

1 Like

script:

here is some of the code(including where the error is from.)

  var timeSince = function(date) {
      //console.log(date);
			if (typeof date !== 'object') {
				date = (new Date(date))/**/;
                //console.log(date);
			}
			var seconds = Math.floor(((new Date() - date) / 1000)/*-50*/);
			var intervalType;
			var interval = Math.floor(seconds / 31536000);
			if (interval >= 1) {
				intervalType = 'year';
			} else {
				interval = Math.floor(seconds / 2592000);
				if (interval >= 1) {
					intervalType = 'month';
				} else {
					interval = Math.floor(seconds / 86400);
					if (interval >= 1) {
						intervalType = 'day';
					} else {
						interval = Math.floor(seconds / 3600);
						if (interval >= 1) {
							intervalType = "hour";
						} else {
							interval = Math.floor(seconds / 60);
							if (interval >= 1) {
								intervalType = "minute";
							} else {
								interval = seconds;
								intervalType = "second";
							}
						}
					}
				}
			}
			if (interval > 1 || interval === 0) {
				intervalType += 's';
			}
            //console.log(interval + ' ' + intervalType);
			return interval + ' ' + intervalType;
		};//made by porter on khan academy https://www.Khanacademy.org/profile/battleboy21
  chatwrapper.style.display = 'none';
  window.not = 0;
var chatfunc = function(data) {
  msgs.innerHTML = "";
  console.log(data);
  for (var chati = 0; chati < data.length; chati++) {
    var msge = document.createElement("div");
    msge.className = "message";
    msge.innerHTML = /*"msg "+(data.length-chati) +*/"<span class = 'timesince'>" + timeSince(data[chati].timesince) +  " ago <span class = \"message-user\"><strong style='color:"+data[chati].col+"'>" + data[chati].user +
      "</strong> said: </span><span class = \"message-text emoji\">" + prettify(data[chati].message) + "</span><hr>";
    msgs.appendChild(msge);
  }
  if (Notification.permission !== "granted")
        Notification.requestPermission();
      else {
        if(!data) return;
        if(loggedin===true&&window.not === 0&&data[0].user!== undefined&&data[0].user !== username&&data[0].user!=="bot"/**/){
          window.not = 1;
        var notification = new Notification(data[0].user , {
          icon: 'https://www.khanacademy.org/computer-programming/logo/5933229681180672/5707702298738688.png',
          body: data[0].message,
        });
      }/*/**/
  }
};
var chatfunc2 = function(data) {
  msgs2.innerHTML = "";
  //console.dir(data);
  for (var chati = 0; chati < data.length; chati++) {
    var msge2 = document.createElement("div");
    msge2.className = "message2";
    msge2.innerHTML = /*"msg "+(data.length-chati) +*/"<span class = 'timesince'>" + timeSince(data[chati].timesince) +  " ago <span class = \"message-user\"><strong style='color:"+data[chati].col+"'>" + data[chati].user +
      "</strong> said: </span><span class = \"message-text emoji\">" + prettify(data[chati].message) + "</span><hr>";
    msgs2.appendChild(msge2);
  }
  if (Notification.permission !== "granted")
        Notification.requestPermission();
      else {
        if(loggedin===true&&window.not === 0&&data[0].user!== undefined&&data[0].user !== username&&data[0].user!=="bot"/**/){
          window.not = 1;
        var notification = new Notification(data[0].user , {
          icon: 'https://www.khanacademy.org/computer-programming/logo/5933229681180672/5707702298738688.png',
          body: data[0].message,
        });
      }
  }
};
console.log(ingame,loggedin);
if(ingame===false/*&&loggedin===true*/){
socket.on("message",chatfunc);
}
//sg.on("message",chatfunc2);
chatform.addEventListener("submit", function(e) {
  e.preventDefault();
  console.dir(new Date().toUTCString());
  console.log(window);
  console.log(timeSince(new Date().toISOString()));
  if(cmg.value.length>1){
  socket.emit("message",{
    user: username,
    message: cmg.value,
    /*timesince: new Date().toISOString()*/
  });
  window.not = 0;
  cmg.value = ""; 
  msgs.scroll(0,0);
  }
});
chatform2.addEventListener("submit", function(e) {
  e.preventDefault();
    console.log("submit");
  if(cmg2.value.length>1){
  sg.emit("message2",{
    user: username,
    message: cmg2.value,
    /*timesince: new Date().toISOString()*/
  },LoginRegister.key);
   console.dir({
    user: username,
    message: cmg2.value,
    /*timesince: new Date().toISOString()*/
  });
  window.not = 0;
  cmg2.value = ""; 
  msgs2.scroll(0,0);
  }
});  
var dow = false;
closec.addEventListener("click", function(e){
  e.preventDefault();
  console.log(e);
  if(!dow){
    this.textContent = "^chat^";
    dow = true;
    chatform.style.display = "none";
    chatwrapper.classList.add("slidedow");
    chatwrapper.classList.remove("slideu");
    chatwrapper.style.height = "0px";
  } else {
    this.textContent = "v";
    dow = false;
    chatform.style.display = "block";
    chatwrapper.classList.add("slideu");
    chatwrapper.classList.remove("slidedow");
    chatwrapper.style.height = "20%";
  }
});
closec2.addEventListener("click", function(e){
  e.preventDefault();
  console.log(e);
  if(!dow){
    this.textContent = "^chat^";
    dow = true;
    chatform2.style.display = "none";
    chatwrapper2.classList.add("slidedow");
    chatwrapper2.classList.remove("slideu");
    chatwrapper2.style.height = "0px";
  } else {
    this.textContent = "v";
    dow = false;
    chatform2.style.display = "block";
    chatwrapper2.classList.add("slideu");
    chatwrapper2.classList.remove("slidedow");
    chatwrapper2.style.height = "20%";
  }
});
  var showPage = function (index) {
    for(var i = 0; i < pages.length; i++) {
      pages[i].style.display = "none";
    }
    document.getElementById(index)
      .style.display = "block";
  };
  for(var i = 0; i < sideb.length; i++) {
    var sb = sideb[i];
    sb.addEventListener("click", function (e) {
      e.preventDefault();
      scene = this.href.toLowerCase()
        .split("#")[1];
      showPage(scene);
    });
  }
  rbtn.addEventListener("click", function (e) {
    e.preventDefault();
    page2();
  });
  lform.addEventListener("submit", function (e) {
    e.preventDefault();
    var password = document.getElementById("password");
    var fusername = document.getElementById("username")
      .value;
    var save = document.getElementById("check");
    socket.emit("login attempt", {
      user: fusername,
      pass: window.btoa(hash(password.value))
    });
    socket.on('logged in', function (user) {
      /**/
      Notification.requestPermission()
        .then(function () {
          new Notification('welcome back', {
            body: 'welcome back ' + user,
            icon: notifimg
          });
        });
      username = user;
      localsave("save_checked", "set", JSON.stringify(save.checked));
        localsave("userdata", "set", JSON.stringify({
          name: fusername,
          pass: window.btoa(hash(password.value))
        }));
      draw();
      enter.play();
      loggedin = true;
      //chatwrapper.style.display = 'inline-block';
      //div.style.display = "none";
      //socket.emit ('player joined',user);
      /*cam1.enabled = false;
      cam2.enabled = true;
      player.enabled= true;
      socket.emit ('initialize',user);*/
    });
    socket.on('login failed', function (user) {
      password.style.background = "#E57373";
      warn.innerHTML = "login failed";
      warn.style.display = "inline-block";
      console.log(warn);
    });
  });
  game_join_btn.addEventListener("click", function (e) {
     ingame = true;
    e.preventDefault();
    log("join");
    log(LoginRegister.key);
    if(gselected) {
      log("joining game", username);
      sg.emit("join game room", {
        u: username,
        k: LoginRegister.key
      });
        
    chatdiv.style.display = "inline-block";
    //chatwrapper.style.display = 'inline';
    div.style.display = "none";
      //cam1.enabled = false;
      cam2.enabled = true;
      player.enabled = true;
      //socket.emit('initialize',username);
    }
  });
  bbtn.addEventListener("click", function (e) {
    e.preventDefault();
    page1();
  });
  rform.addEventListener("submit", function (e) {
    e.preventDefault();
    var password = document.getElementById("password-r");
    var email = document.getElementById("email-r");
    var fusername = document.getElementById("username-r");
    if(password.value.length < 6) {
      password.style.background = "#E57373";
      warn2.innerHTML = "to short";
      warn2.style.display = "inline-block";
      console.log(warn2);
    }
    else {
      socket.emit('register', {
        name: fusername.value,
        email: email.value,
        pass: window.btoa(hash(password.value))
      });
      socket.on('registered', function (user) {
        Notification.requestPermission()
          .then(function () {
            new Notification('thank you', {
              body: 'thank u for letting me use the notifications these will help u as u play',
              icon: notifimg
            });
          });
        username = user;
        fade();
        localsave("save_checked", "set", JSON.stringify(save.checked));
          localsave("userdata", "set", JSON.stringify({
            name: fusername.value,
            pass: window.btoa(hash(password.value))
          }));
        //div.style.display = "none";
        loggedin = true;
        //chatwrapper.style.display = 'inline-block';
        /*console.log(loggedin);
        console.log("camchange");
        cam1.enabled = false;
        cam2.enabled = true; 
        player.enabled = true;
        socket.emit ('initialize',user);*/
      });
      socket.on('already used', function (user) {
        fusername.style.background = "#E57373";
        warn2.innerHTML = "the username " + fusername.value + " has already been taken";
        warn2.style.display = "inline-block";
        console.log(warn2);
      });
    }
  });
  /*chatform.addEventListener("submit", function (e) {
    e.preventDefault();
    if(cmg.value.length > 3) {
      chat.unshift({
        user: username,
        message: cmg.value,
        timesince: new Date()
          .toISOString()
      });
      cmg.value = "";
    }
    console.log("new message " + JSON.stringify(chat) + " " + chat.length);
  });*/
  game_create_btn.addEventListener("click", function (e) {
    ingame = true;
    e.preventDefault();
    cam2.enabled = true;
    player.enabled = true;
    LoginRegister.key = username;
    
    //socket.emit ('initialize',username);
    //document.body.style.display = "none";
    chatdiv.style.display = "inline-block";
    chatwrapper.style.display = 'inline';
    div.style.display = "none";
    sg.emit("new game", {
      type: "free for all",
      creator: username,
      map: "courtyard",
      playernum: 1
    });
  });
  socket.on("leaderboard", function (data) {
    lboards.innerHTML = "";
    console.table("leaderboard data: ", data);
    data.sort(function (a, b) {
      return a.monthScore - b.monthScore;
    });
    if(data.length < 25) {
      for(var id = 0; id < 25; id++) {
        var lplace = document.createElement("tr");
        lplace.id = "place";
        lplace.className = "l-tr";
        if(id < data.length) {
          lplace.innerHTML = "<td>" + (id + 1) + "</td><td><img width = '25' height = '25' src = '" + data[id]
            .profileIMG + "' alt = '" + data[id].userName +
            "'s' profile image'/></td><td><a class = 'profilelink' style = 'color:" + data[id].nameCOL +
            "' href = '#" + data[id].userName + "'>" + data[id].userName + "</a></td><td>" + data[id].monthScore +
            "</td>";
        }
        else {
          lplace.innerHTML = "<td>" + (id + 1) +
            "</td><td><img width = '25' height = '25' src = 'https://cdn.glitch.com/20e968ff-97d4-4430-83ad-42189e4f368d%2Favatar_generic.png?1545099848845' alt = 'no one's' profile image'/></td><td><a style = 'color:white' href = '#'>no one" +
            (id + 1 - data.length) + "</a></td><td>null</td>";
        }
        lboards.appendChild(lplace);
      }
    }
    else {
      for(var id = 0; id < data.length; id++) {
        var lplace = document.createElement("tr");
        lplace.id = "place";
        lplace.className = "l-tr";
        lplace.innerHTML = "<td>" + (id + 1) + "</td><td><img width = '25' height = '25' src = '" + data[id].profileIMG + "' alt = '" + data[id].userName +
          "'s' profile image'/></td><td><a class = 'profilelink' style = 'color:" + data[id].nameCOL +
          "' href = '#" + data[id].userName + "'>" + data[id].userName + "</a></td><td>" + data[id].monthScore +
          "</td>";
        lboards.appendChild(lplace);
      }
    }
  });
  sg.on("playtime", function (data) {
    /*console.log(ingame,loggedin);
    console.table(data);
    console.dir(data);*/
    g_list.innerHTML = "";
    var gpc = function(e){
        var id = this.id;
        console.log(e);
        console.log(this.id);
        console.dir(data[id].creator);
        gselected = true;
        g_i_s.pt.innerHTML = "";
        LoginRegister.key = data[id].creator; 
        //gplace.id = data[id].key;
        g_i_s.i.src = maps_imgs[data[id].map];
        g_i_s.t.textContent = data[id].type;
        g_i_s.u.innerHTML = "<a class = 'profilelink' style = 'color:" + data[id].creatorcol + "' href = '#" +data[id].creator + "'>" + data[id].creator + "</a>";
        g_i_s.m.textContent = data[id].map;
        g_i_s.a.textContent = data[id].avgrank;
        g_i_s.p.textContent = data[id].playernum;
        for(var i in data[id].playerdata) {
          var p = data[id].playerdata[i];
          var gplayrst = document.createElement("tr");
          gplayrst.innerHTML = "<td><a class = 'profilelink' style = 'color:" + p.pcol + "' href = '#" + p.user + "'>" + p.user + "</a></td><td>" + p.prank + "</td><td>" + p.score + "</td>";
          g_i_s.pt.appendChild(gplayrst);
        }/**/
  };
    for(var id in data) {
      var cdata = data[id];
      console.dir(cdata.creator);
      var gplace = document.createElement("tr");
      gplace.id = cdata.creator;
      //var gp = document.getElementById("gplace-"+cdata.creator);
      gplace.addEventListener("click",gpc,true);
      console.log(gplace.id);
      gplace.innerHTML = "<td>" + cdata.type + "</td><td><a class = 'profilelink' style = 'color:" + cdata.rcreatorcol + "' href = '#" + cdata.creator + "'>" + cdata.creator + "</a></td><td>" + cdata.map + "</td><td>" + cdata.avgrank + "</td><td>" + cdata.playernum + "</td>";
      g_list.appendChild(gplace);
  }

  });  
  //window.setInterval(chatfunc, 1000); /**/
  log(localsave("save_checked", "get"));
  if(JSON.parse(localsave("save_checked", "get")) === true) {
    log("using saved data", "background:green;");
    log(JSON.parse(localsave("userdata", "get")));
    socket.emit("login attempt", {
      user: JSON.parse(localsave("userdata", "get"))
        .name,
      pass: JSON.parse(localsave("userdata", "get"))
        .pass
    });
    socket.on('logged in', function (user) {
      username = user;
      login.style.display = "none";
      //div.style.display = "none";
      chatwrapper.style.display = "inline-block";
      loggedin = true;
      //console.log(loggedin);
      //app.fire("loggedin");
      //console.log("camchange");
      //cam1.enabled = false;
      //cam2.enabled = true;
      //player.enabled = true;
      //socket.emit ('initialize',user);
      mainpg.style.display = "block";
    });
  }
  sg.on("message2", function(data){
    chatfunc2(data);
  });
  sg.on("in game players", function (data) {
    chatfunc2(data.chat);
    self.inital(data);
    //console.log("message recived");
    //console.table(data.chat);
  });
  sg.on("ingame players moved", function (data,g,chat) {
    //console.log(data,g,chat);
    self.updateplayers(data,g);
    //console.log("moved");
    chatfunc2(chat);
  });
  sg.on("leave", function (data) {
      console.log(this.init,data);
    self.removePlayer(data);
    });
};

LoginRegister.prototype.inital = function (data) {
  //console.log("joining");
  //console.table(data);
  this.igplayers = data.onlineplayers;
  //console.log(this.init);
  if(!this.init) {
    LoginRegister.gid = data.id;
  }
  for(var id in this.igplayers) {
    //console.log(id);
    //this.igplayers[id].deleted = false;
    if(this.igplayers[id].id !== LoginRegister.gid) {
     // console.log("creating player entity");
      this.igplayers[id].entity = this.createplentity(this.igplayers[id]);
      //console.log(this.igplayers[id].entity);
      //console.log(this.igplayers);
      //console.log(this.igplayers[id]);
    }
  }
  this.init = true;
};
LoginRegister.prototype.createplentity = function (data) {
  var newpl = this.other.clone();
  newpl.enabled = true;
  this.other.getParent().addChild(newpl);
  this.plname = newpl.findByName('player name');
  this.plname.element.text = data.user;
  //console.log("creating player entity ",newpl);
  if(data) {
    newpl.rigidbody.teleport(data.x, data.y, data.z);
    //newpl._children[1].setEulerAngles(data.rx-180, data.ry, data.rz);
  }
  return newpl;
};
LoginRegister.prototype.updateplayers = function (data,g) {
    //console.log(data);
    //console.log(this.igplayers);
    //console.log(this.igplayers[data.id]);
    //console.log(g);
    if(this.init /* */ && data.id !== LoginRegister.gid/* && !this.igplayers[data.id].deleted&&this.igplayers[data.id].entity!==null*/ ) {
    
    //console.log(this.igplayers[data.id].entity);
    //this.igplayers[data.id].entity.rigidbody.teleport(data.x, data.y, data.z);
    this.igplayers[data.id].entity.rigidbody.teleport(data.x, data.y, data.z, 0, data.ry, 0);
    //this.igplayers[data.id].entity._children[1].setLocalEulerAngles(data.rx, 90, data.rz);
  }
};
LoginRegister.prototype.removePlayer = function (data) {
  console.log("removing player");
  console.log(this.igplayers);
  console.log(this.igplayers[data]);
  console.log(this.igplayers[data].entity);
  if(this.igplayers[data].entity && data !== LoginRegister.gid&&this.init) {
    this.igplayers[data].entity.destroy();
    delete this.igplayers[data];
  }
};
LoginRegister.prototype.update = function (dt) {
  if(this.init) {this.updatePosition();}
};
LoginRegister.prototype.updatePosition = function () {
    /*console.log(this.init);*/
    //console.log(LoginRegister.key);
    if (this.player !== undefined) {
    var rot = this.player._children[0].getEulerAngles();
    var pos = this.player.getPosition();
          //log(game_room_key);
    LoginRegister.sg.emit('player moved', {
      id: LoginRegister.gid,
      x: pos.x,
      y: pos.y,
      z: pos.z,
      rx: rot.x,
      ry: rot.y,
      rz: rot.z,
      score: 0
    }, LoginRegister.key);
  
}
};

the code in the script the error is from, and the screenshot of the error.

Is it your own game @quantumgames?

no i am collaborating with @savage_pixels he asked me to help him.

I see the project is recently forked from another project. If you still want to continue with someone else’s project, it will take some effort to find out how the project is set up and what causes the error.

1 Like

ok thanks

Hello!
I need help with my battle royale game. I have everything set up, the shooting, the server, etc. but when a player shoots at another player, the player that is shot at does not die. could someone please help me figure out how to make it so that after one player shoots another player 5 times, the player that got shot goes back to the login screen? the link to the project is here: https://playcanvas.com/editor/scene/1117154 any help is greatly appreciated.

1 Like

well itz kinda OUR battle royal game but ya Lol.

1 Like

yes. lol