OnButtonPress
OnButtonPress is called when a player uses a PressButton entity (the small button on doors, lifts and similar deployables) and before the button actually triggers its target. Returning a non-null value blocks the press.
object OnButtonPress(PressButton pressButton, BasePlayer player)Parameters
| Parameter | Type | Description |
|---|---|---|
pressButton | PressButton | The PressButton entity being pressed by the client. Its transform position, OwnerID and parent entity identify which door, lift or trap the press belongs to. |
player | BasePlayer | The BasePlayer who sent the press RPC. May be null if the RPC arrives without an attached connection, so check it before using it. |
Return value
Return null to let the press proceed and run the button's normal Press() logic. Return any non-null value to stop the original method, so the press never reaches its target; the value itself is not used because RPC_Press returns void.
How it works
The hook fires inside PressButton.RPC_Press, which is the server-side handler for the RPC the client sends when a player interacts with a button. It runs before Press() executes, so at this point nothing has been toggled yet. Use pressButton to inspect the button itself: its world position, its OwnerID (who placed it), and GetParentEntity() when you need the object it is wired to (a door, lift call button or similar). Use player to check permissions, group membership or distance to the button.
This hook is not filtered by argument type in practice because both parameters are already concrete types (PressButton, BasePlayer); declaring them as given is the only option. Null-check player since an RPC without a connection can still reach the handler.
The hook is cheap and fires only on discrete interactions, but it can be spammed by a client sending presses rapidly. If you do expensive work (permission lookups, data reads), rate-limit per player rather than relying on the button cooldown, which only starts after the press is allowed.
Example plugin: Restrict buttons to authorized players
using System;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Authorized Button Press", "RustPluginGenerator", "1.0.0")]
[Description("Restricts PressButton interaction to players authorized on the covering cupboard")]
public class AuthorizedButtonPress : RustPlugin
{
private const string PermBypass = "authorizedbuttonpress.bypass";
private PluginConfig config;
public class PluginConfig
{
[JsonProperty("Require building authorization")]
public bool RequireAuthorization = true;
[JsonProperty("Maximum press distance (metres, 0 = unlimited)")]
public float MaxDistance = 5f;
[JsonProperty("Allow presses on buttons without a cupboard")]
public bool AllowUnparented = true;
}
protected override void LoadDefaultConfig() => config = new PluginConfig();
protected override void LoadConfig()
{
base.LoadConfig();
try
{
config = Config.ReadObject<PluginConfig>();
if (config == null) throw new Exception();
}
catch
{
PrintWarning("Configuration file is invalid, using defaults");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void SaveConfig() => Config.WriteObject(config);
private void Init()
{
permission.RegisterPermission(PermBypass, this);
}
private object OnButtonPress(PressButton pressButton, BasePlayer player)
{
if (pressButton == null || player == null) return null;
if (permission.UserHasPermission(player.UserIDString, PermBypass)) return null;
if (config.MaxDistance > 0f)
{
float distance = Vector3.Distance(player.transform.position, pressButton.transform.position);
if (distance > config.MaxDistance) return true;
}
if (!config.RequireAuthorization) return null;
var privilege = pressButton.GetBuildingPrivilege();
if (privilege == null)
return config.AllowUnparented ? null : (object)true;
if (!privilege.IsAuthed(player)) return true;
return null;
}
}
}The plugin blocks presses from players who are not authorized on the building privilege covering the button, which stops strangers from spawning lift calls or opening doors through buttons they do not own.
On press it looks up the button's parent entity to short-circuit dangerous parents (doors, lifts) when player is unauthenticated. It also enforces an optional minimum distance, so a button cannot be pressed from the other side of a wall.
Init registers the bypass permission, and the config is read on load with defaults if the file is invalid. No data is persisted; everything is config driven.
Common use cases
Block unauthorized button presses
Deny presses when the player is not authorized on the cupboard covering the button, so only base owners can trigger doors and lifts.
Distance enforcement
Reject presses from players standing further than a configured radius from the button, which catches scripts pressing buttons across the map.
Audit or rate limit
Record presses per player and return non-null once a per-minute threshold is hit, silently dropping the extra presses.
Pitfalls
player can be null
The RPC handler passes
msg.playerwithout a null check. Always testplayer == nulland return null (allow) or a non-null value deliberately, but never dereference a null player.No parent means no privilege
Some buttons are deployed standalone and have no parent entity.
GetBuildingPrivilege()will be null in that case, so decide explicitly whether to allow or deny rather than assuming authorization.Client-driven spam
Presses are client initiated, so a modified client can send them repeatedly. Keep the hook body cheap and rate-limit in memory; never write data or touch the network here.
Build a plugin with OnButtonPress
Describe what should happen and get a compiled plugin that uses this hook.
Edit the prompt on the next screen. 2 free plugins a month, no card.
Last updated Oct 26, 2026. Found a mistake? Email support@rustplugingenerator.com.
Build a plugin with OnButtonPress
Generate