An Error Pops Up everytime this script call is running, Where is the mistake?

Hello everyone!
I recently made a Coin Pickup system with 2 players in mind.

The way it works is the following:
- Coin Appears
- Coin Checks in a parrallel process if 1 of the 2 specified events runs on it
- Event continues depending who picked it up with Self-Switches

Here is the code:

Code:
var coinEvents = $gameMap.events().filter(function(event) { return event.event().name === 'coin' && !event._erased; });
var player1Event = $gameMap.events().filter(function(event) { return event.eventId() === 2; })[0];
var player2Event = $gameMap.events().filter(function(event) { return event.eventId() === 3; })[0];
coinEvents.forEach(function(coin) {
  if (player1Event.x === coin.x && player1Event.y === coin.y) {
    $gameSelfSwitches.setValue([$gameMap.mapId(), coin.eventId(), 'A'], true);
    // code for scoring here
  }
  if (player2Event.x === coin.x && player2Event.y === coin.y) {
    $gameSelfSwitches.setValue([$gameMap.mapId(), coin.eventId(), 'B'], true);
  }
}

Here is the error I get with it:
Code:
SyntaxError: Unexpected end of input {stack: (...), message: "Unexpected end of input"}

Can someone help me please? If you have any questions, I'll be happy to specify! Thank you!

I've moved this thread to JavaScript Support. Thank you.

Sorry about that 😛
where do you place this code?
If you placed it in a script command box, are there any unintended linebreaks?
+ this (you need to close your forEach)
Code:
);

Unexpected token eof «undefined», expected punc «,» (line: 12, col: 2)
@Andar I put that code in my coin event. It is a parallel process with only this code in the first page. It is supposed to check if event 2 or 3 goes on it
http://jslint.com/ :kaosalute:

Expected ')' to match '(' from line 3 and instead saw '(end)'.

Code:
forEach(function(){
});

You have
Code:
forEach(function(){
}
Thank you! It is now working as intended 😛

Original thread: https://forums.rpgmakerweb.com/threads/an-error-pops-up-everytime-this-script-call-is-running-where-is-the-mistake.77089/