Skip to content
RustPluginGenerator

CanPickupLock

CanPickupLock is called when a player tries to pick up a deployed lock, before the item is created and given to them. Returning a non-null value blocks the pickup.

Signature
object CanPickupLock(BasePlayer player, BaseLock lockValue)

Parameters

Hook parameters
ParameterTypeDescription
playerBasePlayerThe BasePlayer who sent the take-lock RPC, i.e. the player trying to pick the lock up into their inventory.
lockValueBaseLockThe BaseLock entity (CodeLock, KeyLock, etc.) being removed. Use its skinID, OwnerID or attached parent entity to apply rules.

Return value

Returning null lets the pickup proceed normally. Returning any non-null value (for example false or a string) stops RPC_TakeLock early, so no lock item is created and the lock stays deployed. The returned value itself is not used by the game.

How it works

The hook fires inside BaseLock.RPC_TakeLock, the RPC the client sends when a player holds the interact key on a lock with the take option. It runs after two built-in gates: the player must pass CanInteract() and the lock must have CanRemove set. At that point Oxide calls the hook before IsLocked() is checked and before the replacement item is created with ItemManager.CreateByItemID/CreateByName and handed to the player. The player argument is the initiator, and lockValue is the concrete lock entity, so you can read OwnerID, skinID, IsLocked(), or walk GetParentEntity() to see which door, box or turret it is attached to. Both arguments can be null in theory, so guard them. Because the hook only intercepts the pickup path, it does not fire when a lock is destroyed, when it breaks from damage, or on unlock attempts. Declaring player as a more specific type does not help here since the argument is always a BasePlayer, but narrowing lockValue to CodeLock would make Oxide skip calls for key locks and other lock types.

Example plugin: Block lock pickup on other players' doors

LockPickupGuard.cs
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;

namespace Oxide.Plugins
{
    [Info("Lock Pickup Guard", "RustPluginGenerator", "1.0.0")]
    [Description("Prevents players from picking up locks attached to entities they do not own")]
    public class LockPickupGuard : RustPlugin
    {
        private const string PermBypass = "lockpickupguard.bypass";

        private PluginConfig config;

        private class PluginConfig
        {
            [JsonProperty("Protect foreign locks")]
            public bool ProtectForeignLocks = true;

            [JsonProperty("Allow admins to bypass")]
            public bool AdminsBypass = 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);

        protected override void LoadDefaultMessages()
        {
            lang.RegisterMessages(new Dictionary<string, string>
            {
                ["NotOwner"] = "You cannot take this lock: it does not belong to you."
            }, this);
        }

        private string Msg(string key, string userId = null) => lang.GetMessage(key, this, userId);

        private void Init()
        {
            permission.RegisterPermission(PermBypass, this);
        }

        private object CanPickupLock(BasePlayer player, BaseLock lockValue)
        {
            if (!config.ProtectForeignLocks || player == null || lockValue == null) return null;

            if (config.AdminsBypass && player.IsAdmin) return null;
            if (permission.UserHasPermission(player.UserIDString, PermBypass)) return null;

            var parent = lockValue.GetParentEntity();
            if (parent == null) return null;

            if (parent.OwnerID == player.userID.Get()) return null;

            player.ChatMessage(Msg("NotOwner", player.UserIDString));
            return false;
        }
    }
}

The plugin denies lock pickup when the lock sits on a building the player does not own. On pickup it reads lockValue.GetParentEntity() to find the door or box the lock is attached to, then compares OwnerID against the requester. If the owner differs and the player is not the server admin and lacks the lockpickupguard.bypass permission, the hook returns false and the lock stays in place. A reason is sent through the language file so the player knows why nothing happened. Admins and permitted players keep normal behaviour, and a config toggle switches the entire rule off without unloading the plugin.

Common use cases

  • Raid protection for locks

    Stop players from quietly taking locks off doors or boxes they do not own, forcing them to break the lock with damage like a raid requires.

  • Keep locks on public infrastructure

    Refuse pickup when the lock is attached to an entity with OwnerID 0 and a specific skinID, so event or shop locks cannot be stolen.

  • Pickup cooldown after deploy

    Track when a lock was placed and block pickup for a short window to prevent bait-and-switch tricks with locks during combat.

Pitfalls

  • RPC only, not destruction

    CanPickupLock does not fire when a lock is destroyed by damage or removed by an admin tool. If you need to react to lock loss, use OnEntityKill as well.

  • Parent can be null

    A lock that is somehow not attached to an entity returns null from GetParentEntity(). Always null-check before reading OwnerID or sending messages.

  • Do not spam chat

    The RPC can be sent repeatedly by a client. Keep any rejection message short and consider rate limiting text output per player instead of printing on every attempt.

Build a plugin with CanPickupLock

Describe what should happen and get a compiled plugin that uses this hook.

Prompt: Raid protection for locks using the CanPickupLock hook, with a config file and a permission
Generate this plugin

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 CanPickupLock

Generate