CanChangeCode
CanChangeCode is called when a player submits a new keypad code on a CodeLock, before the game applies it. Returning a non-null value cancels the change.
object CanChangeCode(BasePlayer player, CodeLock codeLock, string text, bool flag)Parameters
| Parameter | Type | Description |
|---|---|---|
player | BasePlayer | The BasePlayer who sent the code change request. This is the player looking at the codelock UI, so you can check their permissions, team membership or ownership here. |
codeLock | CodeLock | The CodeLock entity whose code is being changed. Use it to read the lock's owner (OwnerID), its position, or the entity it is attached to via GetParentEntity(). |
text | string | The four-digit code the player typed into the keypad. Already validated as 4 numeric characters before the hook fires. |
flag | bool | Whether the request is the initial code set (false) or a change of an existing code (true). A change requires the lock to already have a code set. |
Return value
Returning null lets the game continue and store the new code. Returning any non-null value stops the original method at the hook call and returns immediately, so the code change is discarded silently.
How it works
The hook fires inside CodeLock.RPC_ChangeCode, which runs when a client uses the keypad UI to set or change a lock's code. It runs after the game has parsed the code string and the change flag, and after the built-in checks (lock not locked, four digits, numeric only, no change on an uncoded lock) pass. Your method sees the submitting player, the codeLock entity, the four-character text code and the flag that separates first-time setup from an edit. Because the lock entity is passed directly you can read OwnerID and the parent entity without casting. Declaring the first parameter as BasePlayer is required since the caller passes a player object; narrowing it further is not meaningful here. Return anything non-null to block the request; the lock keeps its old code and the client gets no confirmation.
Example plugin: Restrict code changes to the lock owner
using System.Collections.Generic;
using System;
using Newtonsoft.Json;
using Oxide.Core;
namespace Oxide.Plugins
{
[Info("CodeLock Owner Only", "RustPluginGenerator", "1.0.0")]
[Description("Only allows the player who placed a CodeLock to set or change its code.")]
public class CodeLockOwnerOnly : RustPlugin
{
private const string PermBypass = "codelockowneronly.bypass";
private PluginConfig config;
private class PluginConfig
{
[JsonProperty("Allow admins to change any code")]
public bool AllowAdmins { get; set; } = 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"] = "Only the owner of this code lock can change its code."
}, this);
}
private void Init()
{
permission.RegisterPermission(PermBypass, this);
}
private object CanChangeCode(BasePlayer player, CodeLock codeLock, string text, bool flag)
{
if (player == null || codeLock == null) return null;
if (player.userID == codeLock.OwnerID) return null;
if (config.AllowAdmins && player.IsAdmin) return null;
if (permission.UserHasPermission(player.UserIDString, PermBypass)) return null;
player.ChatMessage(lang.GetMessage("NotOwner", this, player.UserIDString));
return true;
}
}
}The plugin overrides CanChangeCode so only the player who placed the CodeLock can set or edit its keypad code. It compares player.userID with the lock's OwnerID and additionally lets authorized players through when the codelockowneronly.bypass permission is granted. When the request is refused it sends a chat message and returns a non-null value to cancel the change; otherwise it returns null so the game stores the new code normally. A config field toggles whether admins are exempt, so server staff can still manage locks without the permission.
Common use cases
Ownership enforcement on locks
Block teammates or raiders who guessed a code from rewriting it, so only the placer can rotate the keypad.
Anti-grief code logging
Compare the submitted
textagainst known patterns or a banned list and refuse weak codes such as 1234 or the player's own Steam ID suffix.Event and raid restrictions
During timed PvP events, deny code changes on locks placed before the event started so captured bases cannot be resecured.
Pitfalls
No feedback by default
Returning a value cancels the change but gives the client nothing, so send a chat or console message yourself or players will think the keypad is broken.
Locked locks never reach the hook
The game already returns early when
IsLocked()is true, soCanChangeCodeonly runs for unlocked locks with a valid four-digit numeric code.Empty codes are filtered earlier
The length and numeric checks run before the hook call, so you never need to validate
textfor digits; treat it as a four-character code.
Build a plugin with CanChangeCode
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 CanChangeCode
Generate