• Studio

  • Studio API

  • Bots

  • Web API

  • Designer Resources

  • Host Resources

  • RemoteFunction

    The RemoteFunction class in represents a remote function that can be invoked and heard on the client and server side. It enables communication between server and client scripts by allowing remote calls to be made and handled with params that you define yourself.

      -- Client and Server script:
    
      local remoteFunction = RemoteFunction.new("TestRemoteFunction");
      local lastSendTime = 0;
      local lastPlayer = nil;
    
      function self:ClientStart()
          -- Get the tap handler on ourselves and invoke server on the remote function when tapped with a custom callback message
          self.gameObject:GetComponent(TapHandler).Tapped:Connect(function()
              print ("Tapped")
              remoteFunction:InvokeServer("Hello from client!", function(response)
                  print(response)
              end)
          end)
    
          -- when the client is invoked, print the server's message
          remoteFunction.OnInvokeClient = function(message)
              print (message)
              return true;
          end
      end
    
      function self:ServerStart()
          server.PlayerConnected:Connect(function(player)
              lastPlayer = player
          end)
    
          remoteFunction.OnInvokeServer = function(message)
              print (message)
              return "Hello from Server!";
          end
      end
    
      function self:ServerUpdate()
          lastSendTime = lastSendTime + Time.deltaTime;
          if lastSendTime > 2 then
              remoteFunction:InvokeClient(lastPlayer, "Unsolicited message from server!", function(response)
                  print(tostring(response))
              end)
              lastSendTime = 0
          end
      end
    

    Properties

    name

    string

    The name of the remote function.

    Callback function for the client that is triggered by the server. Parameters are determined by what was passed into InvokeClient.

    ServerOnly

    Callback function for the server that is triggered by the client. Parameters are determined by what was passed into InvokeServer.

    Methods

    ServerOnly

    Invokes the remote function on the client side.

    Parameters

    player
    Player

    The player to invoke the function for.

    ...

    Add any number of other additional parameters to your invoked function, just remember to handle them in OnInvokeClient.

    Invokes the remote function on the server side.

    Parameters

    ...

    Add any number of other additional parameters to your invoked function, just remember to handle them in OnInvokeServer.

    Updated 2 months ago

    PocketWorlds Icon

    © 2024 Pocket Worlds. All rights reserved.