CanAffordApartmentMasterKey
CanAffordApartmentMasterKey is called when a player asks an apartment security NPC whether they can afford a master key, before the vanilla scrap check runs. Returning a bool overrides the vanilla affordability result.
object CanAffordApartmentMasterKey(BasePlayer player, NPCApartmentSecurity nPCApartmentSecurity)Parameters
| Parameter | Type | Description |
|---|---|---|
player | BasePlayer | The BasePlayer who is asking the apartment security NPC whether they can afford a master key. Null-check it before using its inventory or identity. |
nPCApartmentSecurity | NPCApartmentSecurity | The NPCApartmentSecurity NPC entity running the conversation. Use it to read the NPC's position, owner or net identity when your logic depends on which apartment building is involved. |
Return value
Return true to tell the NPC the player can afford a master key and false to deny it; either stops the vanilla scrap check. Returning null (or any non-bool) leaves the original affordability logic in place.
How it works
This hook fires inside NPCApartmentSecurity.Conversation_CanAffordMasterKey(BasePlayer), which the apartment security NPC runs during its conversation flow when the player requests a master key. The call happens before the vanilla code looks up the scrap item definition and compares the player's scrap amount against the master key price, so it is the earliest point where you can change the outcome.
player is the requesting BasePlayer and nPCApartmentSecurity is the NPC. Both can be null in theory, so guard them. If your hook returns a bool, Oxide stops the original method and uses your value as the result: true makes the NPC treat the player as able to afford the key, false denies it. Any other return value (including null) lets the vanilla scrap check proceed unchanged.
Because the hook only decides affordability, it does not itself grant the key. Charging a different currency, giving free keys to a permission group, or blocking key purchases during a raid are all realistic uses. Do not do heavy work here; the check can run on each conversation step for a player.
Example plugin: Free master keys for a permission group
using System;
using Newtonsoft.Json;
using Oxide.Core;
namespace Oxide.Plugins
{
[Info("ApartmentKeyPerks", "RustPluginGenerator", "1.0.0")]
[Description("Grants free apartment master keys to players holding a permission")]
public class ApartmentKeyPerks : RustPlugin
{
private const string PermFree = "apartmentkeyperks.free";
private PluginConfig config;
private class PluginConfig
{
[JsonProperty("Free keys enabled")]
public bool FreeKeysEnabled { 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);
private void Init()
{
permission.RegisterPermission(PermFree, this);
}
private object CanAffordApartmentMasterKey(BasePlayer player, NPCApartmentSecurity security)
{
if (player == null || security == null) return null;
if (!config.FreeKeysEnabled) return null;
if (!permission.UserHasPermission(player.UserIDString, PermFree)) return null;
return true;
}
}
}The plugin registers a permission (apartmentkeyperks.free) and grants free master keys to anyone who holds it. CanAffordApartmentMasterKey checks whether the requesting player is valid and has the permission; if so it returns true, which bypasses the vanilla scrap check so the key is offered without charging scrap. Otherwise it returns null and the normal price logic runs untouched. A config toggle lets admins disable the perk without unloading the plugin. There is no per-tick work: the hook only responds when a player talks to an apartment security NPC.
Common use cases
VIP free master keys
Grant master keys without scrap to players in a VIP permission group while everyone else pays the normal price. Return
truefor the group andnullotherwise.Disable key purchases during events
Block master key sales globally during a server event or wipe window by returning
false, then restore normal behavior by returningnullonce the event ends.Alternate currency gate
Combine this hook with an economy plugin check so keys are only affordable when the player has enough balance, letting you replace the scrap cost with your own currency.
Pitfalls
Returning a value always overrides vanilla
Any bool you return replaces the scrap check entirely, including for players who would normally pay. Return
nullwhenever you do not want to change the outcome.The hook does not grant the key
It only answers the affordability question. The conversation flow still handles the actual purchase and item handoff, so do not try to give the item here.
Null player or NPC
Guard
playerandnPCApartmentSecuritybefore reading permissions, inventory or position. A null check that returnsnullkeeps vanilla behavior intact.
Build a plugin with CanAffordApartmentMasterKey
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 CanAffordApartmentMasterKey
Generate