Networking

Serverside Code

:::info Serverside Code only works when running local projects - please read that page first.

:::

When compiling a project, if we're a dedicated server, we can run code in #if SERVER blocks.

protected override void OnUpdate()
{
#if SERVER
  Log.Info( $"This is a server update!" );
#else
  Log.Info( $"This is a client update!" );
#endif
}

If we're running on a dedicated server, we'll get This is a server update! spamming our console.

This is especially useful for omitting potentially sensitive code from client builds, like contacting an API server, or a database.

Say you have a GameManager.cs file, which has a partial class, you can accompany it with a GameManager.Server.cs file which is safely omitted from client builds and wraps the whole file in an #if SERVER block - it's a nice bit of sugar that saves you having a bunch of preprocessor blocks in your code.

All serverside code is stripped out of published games by default - so there's no worries there.

Source: Facepunch/sbox-docs (CC-BY-4.0) · updated 2026-09-03. Read it rendered on the official docs.

More in Networking

Community wiki — not affiliated with Facepunch Studios. For the latest and most authoritative information, see the official developer wiki.