Index: code/trunk/src/modules/objects/Script.cc
===================================================================
--- code/trunk/src/modules/objects/Script.cc	(revision 7463)
+++ code/trunk/src/modules/objects/Script.cc	(revision 7474)
@@ -35,8 +35,17 @@
 #include "core/LuaState.h"
 #include "core/XMLPort.h"
+#include "network/Host.h"
+#include "network/NetworkFunction.h"
+
+#include "PlayerManager.h"
+#include "infos/PlayerInfo.h"
+#include "interfaces/PlayerTrigger.h"
+#include "worldentities/pawns/Pawn.h"
 
 namespace orxonox
 {
     CreateFactory(Script);
+
+    registerMemberNetworkFunction(Script, execute);
 
     // Initializing constants.
@@ -51,5 +60,5 @@
         The creator of this object.
     */
-    Script::Script(BaseObject* creator) : BaseObject(creator)
+    Script::Script(BaseObject* creator) : BaseObject(creator), Synchronisable(creator)
     {
         RegisterObject(Script);
@@ -63,4 +72,7 @@
         this->needsGraphics_ = false;
 
+        this->counter_ = 0.0f;
+
+        this->registerVariables();
     }
 
@@ -92,9 +104,10 @@
         XMLPortParam(Script, "times", setTimes, getTimes, xmlelement, mode).defaultValues(Script::INF);
         XMLPortParam(Script, "needsGraphics", setNeedsGraphics, getNeedsGraphics, xmlelement, mode).defaultValues(false);
+        XMLPortParam(Script, "forAll", setForAll, isForAll, xmlelement, mode).defaultValues(false);
 
         XMLPortEventSink(Script, BaseObject, "trigger", trigger, xmlelement, mode);
 
-        if(this->isOnLoad()) // If the object is onLoad the code is executed at once.
-            this->execute();
+        if(this->isOnLoad()) // If the object is onLoad the code is executed at once for all clients.
+            this->execute(0);
     }
 
@@ -116,37 +129,151 @@
     /**
     @brief
+        Register the variables that need to be synchronized.
+    */
+    void Script::registerVariables(void)
+    {
+        registerVariable(code_, VariableDirection::ToClient);
+        registerVariable(needsGraphics_, VariableDirection::ToClient);
+        registerVariable(modeStr_, VariableDirection::ToClient, new NetworkCallback<Script>(this, &Script::modeChanged));
+    }
+
+    void Script::modeChanged(void)
+    {
+        this->setMode(this->modeStr_);
+    }
+
+    void Script::tick(float dt)
+    {
+        SUPER(Script, tick, dt);
+
+        if(!this->clientCallbacks_.empty())
+        {
+            if(this->counter_ < 2.0f)
+            {
+                this->counter_ += dt;
+            }
+            else
+            {
+                for(std::vector<unsigned int>::iterator it = this->clientCallbacks_.begin(); it != this->clientCallbacks_.end(); it++)
+                {
+                    this->execute(*it, true);
+                }
+                this->clientCallbacks_.clear();
+                this->counter_ = 0.0f;
+            }
+        }
+    }
+
+    /**
+    @brief
         Is called when an event comes in trough the event port.
     @param triggered
         Whether the event is triggering or un-triggering.
-    */
-    void Script::trigger(bool triggered)
-    {
-        if(triggered) // If the event is triggering (instead of un-triggering) the code of this Script  is executed.
-            this->execute();
-    }
-
-    /**
-    @brief
-        Executes the Scripts code, depending on the mode.
-    */
-    void Script::execute()
-    {
-        if(this->times_ != Script::INF && this->remainingExecutions_ == 0)
-            return;
-
-        // If the code needs graphics to be executed but the GameMode doesn't show graphics the code isn't executed.
-        if(this->needsGraphics_ && !GameMode::showsGraphics())
-            return;
-
-        if(this->mode_ == ScriptMode::normal) // If the mode is 'normal'.
-            CommandExecutor::execute(this->code_);
-        else if(this->mode_ == ScriptMode::lua) // If it's 'lua'.
-        {
-            assert(this->luaState_);
-            this->luaState_->doString(this->code_);
-        }
-
-        if(this->times_ != Script::INF)
-            this->remainingExecutions_--;
+    @param trigger
+        The object that caused the event to be fired.
+    @return
+        Returns true if successful.
+    */
+    bool Script::trigger(bool triggered, BaseObject* trigger)
+    {
+        if(!triggered || !this->isActive()) // If the Script is inactive it cannot be executed.
+            return false;
+
+        COUT(4) << "Script (&" << this << ") triggered." << std::endl;
+
+        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
+        Pawn* pawn = NULL;
+
+        // If the trigger is a PlayerTrigger.
+        if(pTrigger != NULL)
+        {
+            if(!pTrigger->isForPlayer())  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
+                return false;
+            else
+                pawn = pTrigger->getTriggeringPlayer();
+        }
+        else
+            return false;
+
+        if(pawn == NULL)  //TODO: Will this ever happen? If not, change in NotificationDispatcher as well.
+        {
+            COUT(4) << "The Script was triggered by an entity other than a Pawn. (" << trigger->getIdentifier()->getName() << ")" << std::endl;
+            return false;
+        }
+
+        // Extract the PlayerInfo from the Pawn.
+        PlayerInfo* player = pawn->getPlayer();
+
+        if(player == NULL)
+        {
+            COUT(3) << "The PlayerInfo* is NULL." << std::endl;
+            return false;
+        }
+
+        this->execute(player->getClientID());
+        return true;
+    }
+
+    /**
+    @brief
+        Executes the Scripts code for the input client, depending on the mode.
+    @param clientId
+        The Id of the client the Script should be executed for.
+    @param fromCallback
+        Whether this method is executed in response to the connectedCallback().
+    */
+    void Script::execute(unsigned int clientId, bool fromCallback)
+    {
+        if(GameMode::isServer())
+        {
+            // If the number of executions have been used up.
+            if(this->times_ != Script::INF && this->remainingExecutions_ == 0)
+                return;
+
+            // Decrement the number of remaining executions.
+            if(this->times_ != Script::INF)
+                this->remainingExecutions_--;
+        }
+
+        if(GameMode::isStandalone() || Host::getPlayerID() == clientId)
+        {
+            // If the code needs graphics to be executed but the GameMode doesn't show graphics the code isn't executed.
+            if(this->needsGraphics_ && !GameMode::showsGraphics())
+                return;
+
+            if(this->mode_ == ScriptMode::normal) // If the mode is 'normal'.
+                CommandExecutor::execute(this->code_);
+            else if(this->mode_ == ScriptMode::lua) // If it's 'lua'.
+            {
+                if(this->luaState_ == NULL)
+                    this->luaState_ = new LuaState();
+                this->luaState_->doString(this->code_);
+            }
+        }
+        if(!GameMode::isStandalone() && GameMode::isServer() && Host::getPlayerID() != clientId)
+        {
+            if(!fromCallback && this->isForAll())
+            {
+                const std::map<unsigned int, PlayerInfo*> clients = PlayerManager::getInstance().getClients();
+                for(std::map<unsigned int, PlayerInfo*>::const_iterator it = clients.begin(); it != clients.end(); it++)
+                {
+                    callMemberNetworkFunction(Script, execute, this->getObjectID(), it->first, it->first, false);
+                }
+            }
+            else
+            {
+                callMemberNetworkFunction(Script, execute, this->getObjectID(), clientId, clientId, false);
+            }
+        }
+    }
+
+    void Script::clientConnected(unsigned int clientId)
+    {
+        if(!GameMode::isStandalone() && GameMode::isServer() && this->isOnLoad())
+        {
+            if(clientId != 0)
+                //TODO: Do better. This is only a temporary fix.
+                this->clientCallbacks_.push_back(clientId);
+        }
     }
 
@@ -160,8 +287,12 @@
     {
         if(mode == Script::NORMAL)
+        {
             this->setMode(ScriptMode::normal);
+            this->modeStr_ = Script::NORMAL;
+        }
         else if(mode == Script::LUA)
         {
             this->setMode(ScriptMode::lua);
+            this->modeStr_ = Script::LUA;
             // Creates a new LuaState.
             if(this->luaState_ == NULL)
@@ -170,6 +301,7 @@
         else
         {
-            COUT(2) << "Invalid mode '" << mode << "' in Script object." << std::endl;
+            COUT(2) << "Invalid mode '" << mode << "' in Script object. Setting to 'normal'." << std::endl;
             this->setMode(ScriptMode::normal);
+            this->modeStr_ = Script::NORMAL;
         }
     }
@@ -210,6 +342,7 @@
         else
         {
-            COUT(2) << "Invalid times '" << times << "' in Script." << std::endl;
+            COUT(2) << "Invalid times '" << times << "' in Script. Setting to infinity." << std::endl;
             this->times_ = Script::INF;
+            this->remainingExecutions_ = Script::INF;
         }
     }
Index: code/trunk/src/modules/objects/Script.h
===================================================================
--- code/trunk/src/modules/objects/Script.h	(revision 7463)
+++ code/trunk/src/modules/objects/Script.h	(revision 7474)
@@ -33,5 +33,10 @@
 
 #include <string>
+#include <vector>
+
 #include "core/BaseObject.h"
+#include "tools/interfaces/Tickable.h"
+#include "network/synchronisable/Synchronisable.h"
+#include "network/ClientConnectionListener.h"
 
 namespace orxonox
@@ -54,6 +59,7 @@
         'code': The code that should be executed.
         'mode': The mode, specifying whether the set code should be executed the normal way ('normal') or in lua ('lua'). Default is 'normal'.
-        'onLoad': Whether the code is executed upon loading (creation) of this object. Default is true.
+        'onLoad': Whether the code is executed upon loading (creation) of this object. If this is set the code is executed ofr all players, regardless of the value of parameter 'forAll'. Default is true.
         'needsGraphics': Whether the code needs graphics to be executed or not. Default is false.
+        'forAll': Whether the code is executed for all players each time the Script is triggered or jut for the player triggering the Script. If forAll is false, which is default, the event that triggers the Script must come from a PlayerTrigger.
 
         Here are two examples illustrating the usage:
@@ -77,5 +83,5 @@
         Damian 'Mozork' Frick
     */
-    class _ObjectsExport Script : public BaseObject
+    class _ObjectsExport Script : public BaseObject, public Synchronisable, public ClientConnectionListener, public Tickable
     {
         public:
@@ -86,6 +92,8 @@
             virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode); //!< Creates a port that can be used to channel events and react to them.
 
-            void trigger(bool triggered); //!< Is called when an event comes in trough the event port.
-            void execute(); //!< Executes the Scripts code, depending on the mode.
+            virtual void tick(float dt);
+
+            bool trigger(bool triggered, BaseObject* trigger); //!< Is called when an event comes in trough the event port.
+            void execute(unsigned int clientId, bool fromCallback = false); //!< Executes the Scripts code for the input client, depending on the mode.
 
             /**
@@ -139,4 +147,20 @@
                 { return this->needsGraphics_; }
 
+            /**
+            @brief Set whether the code is executed for all players or just for the player triggering the Script.
+            @param forAll If true the code is executed for all players.
+            */
+            void setForAll(bool forAll)
+                { this->forAll_ = forAll; }
+            /**
+            @brief Get whether the Script executes its code for all players or just for the player triggering the Script.
+            @return Returns true if the code is executed for all players, false if not.
+            */
+            bool isForAll(void)
+                { return this->forAll_; }
+
+            virtual void clientConnected(unsigned int clientId);
+            virtual void clientDisconnected(unsigned int clientid) {}
+
         private:
             //! Static variables to avoid magic strings.
@@ -150,7 +174,16 @@
             int times_; //!< The number of times the Scripts code is executed at the most. -1 denotes infinity.
             bool needsGraphics_; //!< Whether the code to be executed needs graphics.
+            bool forAll_; //!< Whether the code is executed for all players (in a multiplayer setup) or just for the one triggering the Script.
+
+            std::string modeStr_;
+
+            std::vector<unsigned int> clientCallbacks_;
+            float counter_;
 
             LuaState* luaState_; //!< The LuaState to execute the code in lua.
             int remainingExecutions_; //!< The number of remainign executions. -1 denotes infinity.
+
+            void registerVariables(void);
+            void modeChanged();
 
             /**
