Skip to content
FMFallout Multiplayer Scripting APIScripting guides and API
GuideMaintainer authored

Containers, loot and locked doors

Restock the Commonwealth's own containers on a timer, gate a locked door on a keyholder, refuse a pickup, and keep an audit log of every inventory change.

Fallout 4’s world is already full of containers, doors and loose junk, and every one of them is addressable by its form ID. You will write a scavenging loop around the ones that are already there: a safe that refills every few minutes, a gate only a keyholder can open, pickups that can be refused, and a log of everything that enters or leaves a player’s pockets.

  • The difference between a reference, an item entity and an inventory row.
  • Reading and rewriting a world container with Objects.getContainer and setContainer.
  • Refusing doorChange, containerOpen, itemDrop and itemPickUp, and what the player sees.
  • Spawning loose items into the world with Item.spawn.
  • Auditing with inventoryChange, and why it never fires for your own writes.
scavenge/
package.json
tsconfig.json
src/server/
index.ts imports, and a /loot command
places.ts the references this mode uses
restock.ts containers that refill
locks.ts the keyholder gate
rules.ts pickup and drop rules
audit.ts the log

This is the distinction the whole API turns on, and the one that trips people up:

What it is How you name it Lives in
Reference A thing placed in the world by the game’s own data: a door, a safe, a mailbox A form ID, 0x670b7 The game’s data files
Item A loose item entity the server owns: dropped, or spawned by a script An Item handle, with .id The server
InventoryItem A row in somebody’s pockets, or in a container { form, count } A player or a container

A reference is not an entity. You never get a handle for it; you pass its form ID to Objects and the server reaches it in every player’s game.

src/server/places.ts
/** Placed references in Sanctuary, from Fallout4.esm. */
export const Place = {
/** Loot_Prewar_Safe_Floor, locked at difficulty 25. */
safe: 0x670b7,
/** Loot_Prewar_Suitcase_Clothes. */
suitcase: 0xae137,
/** PicketFenceB_Gate01, a plain door. */
gate: 0x75315,
/** SanctuaryMailbox. */
mailbox: 0xa8826,
} as const;

getContainer answers the rows a reference holds, or null when the form is not a container. setContainer replaces the whole content, so read, decide, write:

src/server/restock.ts
import { Place } from "./places.js";
const EVERY_MS = 180000;
/** Form IDs of what the safe may hold. */
const LOOT = [
{ form: 0x0001f66a, count: 5 },
{ form: 0x00004835, count: 2 },
{ form: 0x0000000f, count: 40 },
];
function restock(ref: number): void {
const current = Objects.getContainer(ref);
if (current === null) {
console.warn(`${ref.toString(16)} is not a container`);
return;
}
// Leave it alone while somebody has it open: setContainer under their cursor is a bad time.
if (Objects.getContainerHolder(ref)) return;
const total = current.reduce((sum, row) => sum + row.count, 0);
if (total > 0) return;
Objects.setContainer(ref, LOOT);
console.log(`Restocked ${ref.toString(16)}`);
}
setInterval(() => {
restock(Place.safe);
restock(Place.suitcase);
}, EVERY_MS);

getContainerHolder is the guard that makes this safe: it answers the player who currently has the container open, or null. Rewriting a container somebody is looking into is how you make items appear to duplicate.

doorChange fires when a player opens or closes a door. Return false and their door swings back, which means the refusal is visible in the world rather than silent:

src/server/locks.ts
import { Place } from "./places.js";
/** The form ID of whatever you decide a key is. */
const KEY = 0x0000000a;
Events.on("doorChange", (player: Player, ref: number, open: boolean) => {
if (ref !== Place.gate) return;
if (!open) return;
if (player.getItemCount(KEY) < 1) {
Chat.sendToPlayer(player, "The gate is chained. You need a key.", { author: "Scavenge" });
return false;
}
Chat.sendToAll(`${player.nickname} opened the gate.`, { author: "Scavenge" });
return true;
});

lockChange is its sibling, for a player picking, locking or unlocking a lock. The server can also act on its own, without being asked:

src/server/locks.ts
import { Place } from "./places.js";
const KEY = 0x0000000a;
Events.on("doorChange", (player: Player, ref: number, open: boolean) => {
if (ref !== Place.gate || !open) return;
if (player.getItemCount(KEY) < 1) {
Chat.sendToPlayer(player, "The gate is chained. You need a key.", { author: "Scavenge" });
return false;
}
return true;
});
Events.on("lockChange", (player: Player, ref: number, locked: boolean) => {
if (ref !== Place.safe || locked) return;
Chat.sendToAll(`${player.nickname} cracked the safe.`, { author: "Scavenge" });
});
/** Shut the whole settlement at nightfall. */
export function lockDown(): void {
Objects.setDoorOpen(Place.gate, false);
Objects.setLocked(Place.gate, true);
Objects.setLocked(Place.safe, true);
}

isDoorOpen and isLocked read the same state back, and both answer null for a form that is neither a door nor lockable, which is the cheapest way to check a form ID is what you think it is.

Both of these fire before the player gets what they asked for, and both can say no. itemPickUp hands you either an Item entity (something dropped or spawned) or a ref (a placed reference lying in the world), never both:

src/server/rules.ts
const CARRY_LIMIT = 150;
function carried(player: Player): number {
return player.getInventory().reduce((sum, row) => sum + row.count, 0);
}
Events.on("itemPickUp", (player: Player, item: Item | null, ref: number | null) => {
if (carried(player) < CARRY_LIMIT) return true;
Chat.sendToPlayer(player, "You are carrying too much.", { author: "Scavenge" });
// The item stays exactly where it is.
return false;
});
Events.on("itemDrop", (player: Player, item: Item) => {
if (item.form !== 0x0000000f) return true;
Chat.sendToPlayer(player, "Caps do not litter well.", { author: "Scavenge" });
// Refused: the item leaves the world and goes back into their inventory.
return false;
});

Item.spawn needs a position and a space. A space is a worldspace or an interior cell, and the easiest correct answer is wherever the player already is:

src/server/drops.ts
export function dropFor(player: Player, form: number, count: number): Item | null {
const space = player.getSpace();
if (!space) return null;
// A little above the player, so it settles rather than clipping into the floor.
const at = player.position.clone();
at.z += 50;
return Item.spawn(form, count, at, space);
}
Events.on("playerDeath", (player: Player) => {
// Spill a handful of caps where they fell.
dropFor(player, 0x0000000f, 25);
});

player.position returns the live vector, so clone() before changing it: adding to the value you were handed is a write to the player’s own position.

inventoryChange reports what the server accepted from a player’s game: loot taken, something bought, something crafted, something used. An InventoryDelta carries the form, the signed change and where it came from:

src/server/audit.ts
Events.on("inventoryChange", (player: Player, changes: InventoryDelta[]) => {
for (const change of changes) {
const verb = change.delta > 0 ? "gained" : "lost";
const from = change.from === null ? "nowhere" : `ref ${change.from.toString(16)}`;
console.log(`${player.nickname} ${verb} ${Math.abs(change.delta)} of ${change.form.toString(16)} from ${from}`);
}
});
src/server/index.ts
import "./restock.js";
import "./locks.js";
import "./rules.js";
import "./audit.js";
import { Place } from "./places.js";
Events.on("playerCommand", (player: Player, command: string) => {
if (command !== "loot") return;
for (const [name, ref] of Object.entries(Place)) {
const rows = Objects.getContainer(ref);
const locked = Objects.isLocked(ref);
const held = rows === null ? "not a container" : `${rows.length} kinds`;
Chat.sendToPlayer(player, `${name}: ${held}${locked === true ? ", locked" : ""}`, { author: "Scavenge" });
}
});
  1. Build and load:

    Server console
    refresh
    ensure scavenge
  2. /tp sanctuary, then /loot to see the state of all four references.

  3. Empty the safe. Within three minutes it refills.

  4. Walk up to the gate without a key. It swings back shut.

  5. Fill your pockets past the limit and try to pick up anything.

  6. Watch the server console while you loot: the audit log names every form that moved.

  • Per player loot. setVisibleTo on a spawned Item restricts it to one player’s game, which is instanced loot in one call.
  • A respawning supply drop. Keep a list of positions, call Item.spawn on a timer, and track the handles so you can destroy() the ones nobody collected.
  • Weight that means something. Replace the row count in carried with a real weight table keyed by form, and refuse the pickup against that instead.