Hi there, I have a problem and can’t find a solution, seems like I am missing something
Scenario:
- I’m joining a server and there are two other players online
- One of those players gets loaded, while other is not, since it doesn’t go trough nearPlayersLoadedLimit
- The loaded player gets very far from player and is unloaded, thus other player gets loaded now
- The problem: When there are 1 loaded player and other player comes into distance, it is loaded again, seems like it doesn’t respond to nearPlayersLoadedLimit and isAllowed = ‘True’ gets attached
var nearPlayers = [];
var nearPlayersLoaded = [];
var nearPlayersAllowedFar = [];
var nearPlayersLoadedLimit = 1;
var closestDistance = 30;
playerPosition = this.player.getPosition();
var otherPlayers = this.app.root.findByTag('otherPlayer');
otherPlayers.forEach( function(otherPlayersEntity){
var distance = playerPosition.distance(otherPlayersEntity.getPosition());
if (distance < closestDistance) {
if (otherPlayersEntity.id) {
otherPlayersEntity.isNear = 'True';
nearPlayers.push(otherPlayersEntity);
//otherPlayersEntity.close = 'False';
}
if (otherPlayersEntity.id && otherPlayersEntity.isNear == 'True' && nearPlayersLoaded.length < nearPlayersLoadedLimit) {
otherPlayersEntity.isAllowed = 'True';
nearPlayersLoaded.push(otherPlayersEntity);
//otherPlayersEntity.close = 'False';
}
}
if (distance > closestDistance) {
if (otherPlayersEntity.id && otherPlayersEntity.wasAllowed == 'True') {
nearPlayersAllowedFar.push(otherPlayersEntity);
//otherPlayersEntity.close = 'False';
}
}
}.bind(this));
console.log(nearPlayers);
console.log(nearPlayersAllowedFar);
console.log(nearPlayersLoaded);
if (this.players[data.id].entity.children[0].animation.data.currAnim !== data.a) {
this.players[data.id].entity.children[0].animation.play(data.a);
//console.log(data.a);
}
var v1 = this.player.getPosition();
var v2 = this.players[data.id].entity.getPosition();
var d = v1.distance(v2);
//console.log("The distance between v1 and v2 is: " + d);
if (d < 30 && this.players[data.id].entity.isAllowed == 'True') {
this.players[data.id].entity.enabled = true;
}
if (d > 30 && this.players[data.id].entity.isAllowed == 'True') {
this.players[data.id].entity.wasAllowed = 'True';
this.players[data.id].entity.isNear = '';
this.players[data.id].entity.isAllowed = '';
this.players[data.id].entity.enabled = false;
}
var nearPlayers = [];
var nearPlayersLoaded = [];
var nearPlayersAllowedFar = [];
var nearPlayersLoadedLimit = 1;
var closestDistance = 30;
playerPosition = this.player.getPosition();
var otherPlayers = this.app.root.findByTag('otherPlayer');
otherPlayers.forEach( function(otherPlayersEntity){
var distance = playerPosition.distance(otherPlayersEntity.getPosition());
if (distance < closestDistance) {
if (otherPlayersEntity.id) {
otherPlayersEntity.isNear = 'True';
nearPlayers.push(otherPlayersEntity);
//otherPlayersEntity.close = 'False';
}
if (otherPlayersEntity.id && otherPlayersEntity.wasAllowed !== "True" && otherPlayersEntity.isNear == 'True' && nearPlayersLoaded.length < nearPlayersLoadedLimit) {
otherPlayersEntity.isAllowed = 'True';
nearPlayersLoaded.push(otherPlayersEntity);
//otherPlayersEntity.close = 'False';
}
if (locked == true) {
console.log('this is executing');
if (otherPlayersEntity.id && otherPlayersEntity.wasAllowed == "True" && otherPlayersEntity.isNear == 'True' && nearPlayersLoaded.length < nearPlayersLoadedLimit) {
otherPlayersEntity.isAllowed = 'True';
otherPlayersEntity.wasAllowed = 'False';
nearPlayersLoaded.push(otherPlayersEntity);
//otherPlayersEntity.close = 'False';
}
}
}
if (distance > closestDistance) {
if (otherPlayersEntity.id && otherPlayersEntity.isAllowed == 'True') {
// otherPlayersEntity.isNear = "";
otherPlayersEntity.wasAllowed = "True";
otherPlayersEntity.isNear = 'False';
otherPlayersEntity.isAllowed = undefined;
//otherPlayersEntity.close = 'False';
}
}
}.bind(this));
console.log(nearPlayers);
console.log(nearPlayersAllowedFar);
console.log(nearPlayersLoaded);
console.log(nearPlayersLoaded.length);
if (nearPlayersLoaded.length < nearPlayersLoadedLimit) {
locked = true;
} else {
locked = false;
}
if (this.players[data.id].entity.children[0].animation.data.currAnim !== data.a) {
this.players[data.id].entity.children[0].animation.play(data.a);
//console.log(data.a);
}
var v1 = this.player.getPosition();
var v2 = this.players[data.id].entity.getPosition();
var d = v1.distance(v2);
//console.log("The distance between v1 and v2 is: " + d);
if (d < 30 && this.players[data.id].entity.isAllowed == 'True' && this.players[data.id].entity.isNear == 'True') {
this.players[data.id].entity.enabled = true;
}
if (d > 30 && this.players[data.id].entity.wasAllowed == 'True' && this.players[data.id].entity.isNear == 'False') {
this.players[data.id].entity.enabled = false;
}
This is it, I dont know what I did here but it works as expected: It will limit all incoming (new and existing) players to presetted variable, in this case:1 (could be set to whatever) by distance
Seems like i have to make a post in this forum to get it working 
Update: Script still fails for existing players, Imagine i’m moving through the world and approaching new players, and they all load
1 Like
Improoved edition: doesn’t fail for new or existing players, more human-readable code
What’s left is, player model loading-unloading, will have to add checks if model loading/unloading finished in between 
const playerLimit = 2;
const nearestPlayers = [];
playerPosition = this.player.getPosition();
var otherPlayers = this.app.root.findByTag('otherPlayer');
otherPlayers.forEach( function(otherPlayersEntity){
if (otherPlayersEntity.id) {
nearestPlayers.push(otherPlayersEntity);
}
}.bind(this));
const enabledPlayers = [];
for (var i = 0; i < nearestPlayers.length; i++) {
var v1 = this.player.getPosition();
var v2 = nearestPlayers[i].getPosition();
var d = v1.distance(v2);
if (d < 30) {
if (enabledPlayers.length < playerLimit) {
nearestPlayers[i].enabled = true;
} else {
nearestPlayers[i].enabled = false;
}
} else {
nearestPlayers[i].enabled = false;
}
if (nearestPlayers[i].enabled == true){
enabledPlayers.push(nearestPlayers[i]);
}
}
2 Likes