OnCodeChange
OnCodeChange is called when a player submits a new code for a ModularCar's built-in lock through the garage/lock UI, before the code is written to the lock.
object OnCodeChange(ModularCar carOccupant, BasePlayer player, string text)Parameters
| Parameter | Type | Description |
|---|---|---|
carOccupant | ModularCar | The ModularCar whose lock code is being changed. Usually the hook fires on the driver seat of the car; use it to check owner, lock state or authorized players. |
player | BasePlayer | The BasePlayer who sent the new code request from the car's lock UI. Check authorization, permission or team membership here. |
text | string | The raw string the player typed as the new code. Validate length, characters or format before the code is applied to the lock. |
Return value
Return null (or nothing) to let the game write the new code through CarLock.TrySetNewCode. Returning any non-null value skips that call entirely, the change effect does not play, and the car keeps its old code. The original method returns void, so the returned object is never used as a value.
How it works
The hook fires inside ModularCarGarage.RPC_RequestNewCode, the RPC that the client sends when a player confirms a new lock code on a modular car. The server has already read the code string from the network message and resolved the sending player, so text is exactly what the client submitted (usually four digits, but the game does not enforce that here). player is the sender, and carOccupant is the car the request was made on.
Returning a non-null value from your hook aborts the code change: CarLock.TrySetNewCode is never called and the lock keeps its previous code. This makes the hook a validation gate rather than a notification — you can reject malformed codes, restrict who may set codes, or block code changes during a raid.
The hook does not fire when a code is entered to open the lock (that is OnCodeEntered) and it does not fire for CodeLock entities on doors. It only covers the car lock. Because the player controls the string, always sanity-check it before allowing it through; nothing stops a modified client from sending an empty or very long value.
Example plugin: Enforce a four-digit numeric car lock code
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
namespace Oxide.Plugins
{
[Info("Vehicle Code Validator", "RustPluginGenerator", "1.0.0")]
[Description("Validates and restricts lock code changes on modular cars")]
public class VehicleCodeValidator : RustPlugin
{
private const string PermBypass = "vehiclecodevalidator.bypass";
private PluginConfig config;
private class PluginConfig
{
[JsonProperty("Required code length")]
public int CodeLength = 4;
[JsonProperty("Require building authorization")]
public bool RequireAuth = 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>
{
["InvalidLength"] = "A car lock code must be exactly {0} digits.",
["NotAuthorized"] = "You are not authorized to change the code on this vehicle."
}, this);
}
private string Msg(string key, string userId = null, params object[] args) => string.Format(lang.GetMessage(key, this, userId), args);
private void Init()
{
permission.RegisterPermission(PermBypass, this);
}
private object OnCodeChange(ModularCar carOccupant, BasePlayer player, string text)
{
if (carOccupant == null || player == null || text == null) return null;
if (permission.UserHasPermission(player.UserIDString, PermBypass)) return null;
if (text.Length != config.CodeLength || !int.TryParse(text, out _))
{
player.ChatMessage(Msg("InvalidLength", player.UserIDString, config.CodeLength));
return true;
}
if (config.RequireAuth)
{
var privilege = carOccupant.GetBuildingPrivilege();
bool authorized = (privilege != null && privilege.IsAuthed(player)) || carOccupant.OwnerID == player.userID;
if (!authorized)
{
player.ChatMessage(Msg("NotAuthorized", player.UserIDString));
return true;
}
}
return null;
}
}
}The plugin validates every car lock code submitted through the modular car lock UI. It rejects anything that is not exactly the configured number of digits (default four) or contains non-numeric characters, blocking the change and sending a chat message to the player. A second config option limits code changes to players who are authorized on the car or have a configurable permission node, which prevents strangers who somehow reached the lock UI from locking the owner out. Messages go through the language system so admins can translate or reword them, and both limits are toggleable in the config file under oxide/config.
Common use cases
Block lockouts during a raid
Return a non-null value when the player is not authorized on the car or its building privilege, so looters cannot overwrite the owner's code mid-raid.
Force a minimum code length
Reject short codes such as '0000' or single digits by validating
text.Lengthbefore the game stores it, improving security on public servers.Log code changes for auditing
Record who changed a car code and when into a data file via Interface.Oxide.DataFileSystem, while returning null so the change still applies.
Pitfalls
The string is client-controlled
textcomes straight from the client RPC, so never trust its length or characters. Validate withint.TryParseor a digit check and cap the length before allowing it.This hook is not OnCodeEntered
OnCodeChange fires only when setting a new code on a modular car. Entering an existing code to open the lock and door CodeLocks use different hooks, so do not expect this hook to fire for them.
Rejecting silently confuses players
When you return a non-null value the lock UI closes without any feedback. Always send a chat message or console reply so the player knows why the code was refused.
Build a plugin with OnCodeChange
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 OnCodeChange
Generate