Pool::forEach: Difference between revisions
m (Replaced HTML with template) |
Almeidowski (talk | contribs) m (Adding a third and easier to understand example) |
||
| Line 44: | Line 44: | ||
</pre> | </pre> | ||
}} | }} | ||
==Example #3== | |||
This example will teleport a player that types the command /port to the player whose name he writes as 1st argument. | |||
{{ServersideCode| | |||
<pre> | |||
mp.events.addCommand('port', (player, name) => { | |||
mp.players.forEach(_player => { | |||
if(_player.name === name) | |||
player.position = _player.position; | |||
}); | |||
}); | |||
</pre> | |||
}} | |||
==See Also== | ==See Also== | ||
{{EntityPool_function}} | {{EntityPool_function}} | ||
Revision as of 11:35, 2 September 2020
This function is used for calling a function for each element in a pool.
Syntax
pool.forEach(Function callingFunction);
Required Arguments
- callingFunction: Function, what will be called.
Example #1
This example will generate text with all player nicknames.
Server-Side
let getNicknamesText = () => {
let text = ``;
mp.players.forEach(
(player, id) => {
text = text == `` ? player.name : `${text} , ${player.name}`;
}
);
return text;
};
let blahBlah = getNicknamesText();
console.log(blahBlah != `` ? blahBlah : `Server not have players.`);
Example #2
This example will add a command to remove all server vehicles by forEach.
Server-Side
mp.events.addCommand(`removeAll`,
(player) => {
mp.vehicles.forEach(
(vehicle) => {
vehicle.destroy();
}
);
mp.players.broadcast(`${player.name} DESTROY ALL CARS!`);
}
);
Example #3
This example will teleport a player that types the command /port to the player whose name he writes as 1st argument.
Server-Side
mp.events.addCommand('port', (player, name) => {
mp.players.forEach(_player => {
if(_player.name === name)
player.position = _player.position;
});
});
See Also
- Functions
- Properties
- Arrays