Server Scripts

What are server scripts and why do we need them?

The main difference between Server scripts and Client scripts is that they are running on the server.

For Client scripts, each player is running their own local version of a script, while for Server scripts, all players are running the same version of a script. Also, server scripts and client scripts do not share the same API.

Most of the time we should be able to achieve our task using Client scripts, specially with the availability of classes like the SNetwork or SPersistence classes, which allow scripts to communicate with each other and share data.

However, we may sometimes run into tasks that are more complex and require an independent script (a server script) to be a central hub for communication, data storage, processing information, syncing etc... A good example of an interactive object that would be done using a server script is a multiplayer board game.

Creating a Server script:

  • Create a GameObject.

  • Add the server script component to the GameObject. (Add Component -> Server Script)

  • Fill in the Class ID field with a unique identifier for your server script. (example: com.example.theserverscript)

  • Drag your .lua file into the Script field.

  • Add a Virtual Good component. (Add Component -> Virtual Good)

  • Most important part in a Server Script's Virtual Good component is that the Type needs to be set to Server Script.

  • Upload the GameObject .

Using a Server script:

  • Create a GameObject.

  • Add a Require Server Script component.

  • Set the Virtual Good ID field to the same virtual good ID of your above created server script.

  • Set the Class ID field to the same class ID of your above created server script.

  • Add a Room Furniture component (Add Component -> Room Furniture)

  • Now this object will load the above created server script in the region it is placed in.

  • You can also add a Scripting Runtime component to this GameObject. (Add Component -> Scripting Runtime).

Events

Unlike client scripting where you have to create hooks/bindings to events, Server scripts only need you to declare that function without creating any hooks/bindings.

For example:

function OnAvatarJoin(id)
Space.Log(id .. " joined the scene.")
end

You can find Server script events here.

Sample Project

The Trivia Box sample project is a good demonstration of how to use server scripts.

You can access it here and download it's open-source files.

Thank You

[This guide is still in progress. Please report issues here]

Last updated

Sinespace® is a registered trademark of Sine Wave Entertainment Ltd, All Rights Reserved.