CanAssignBed
CanAssignBed is called when a player uses a sleeping bag's 'assign to friend' action to set another player's user ID as the bed owner. Returning a non-null value cancels the assignment.
object CanAssignBed(BasePlayer player, SleepingBag sleepingBag, ulong targetUserID)Parameters
| Parameter | Type | Description |
|---|---|---|
player | BasePlayer | The BasePlayer interacting with the sleeping bag's menu, i.e. the player trying to assign the bed to another player's user ID. |
sleepingBag | SleepingBag | The SleepingBag entity being reassigned. Its deployerUserID identifies the original owner and OwnerID is the entity owner. |
targetUserID | ulong | The raw user ID read from the client packet that the player is trying to assign as the bed's new owner. |
Return value
Return a non-null value (commonly true) to cancel the assignment; the original code path returns and the bag's owner is not changed. Returning null lets the game continue with its normal checks and ownership update.
How it works
The hook fires inside SleepingBag.AssignToFriend, after the game has already verified that the player can interact, that they own the bag's deployer, and that friend reassignment is allowed. Just before any of those checks would change the bag's ownership, Oxide reads the target user ID from the client message and calls this hook with the interacting player, the bag, and the requested user ID.
Because the hook only receives player, sleepingBag and a ulong, there is no more specific type to declare for dispatch purposes; every call passes a BasePlayer and a SleepingBag. Use targetUserID to look up the target with BasePlayer.FindByID (which also finds sleeping players) or to compare it against team members, and use sleepingBag.OwnerID or deployerUserID to confirm who currently controls the bed.
Returning non-null aborts the whole assign action, so the client's request is silently dropped and the bag keeps its old owner. The hook does not fire for placing a new bag or for binding yourself; it is specific to the friends-assignment flow.
Example plugin: Restrict friend bed assignment to teammates
using System.Linq;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Bed Assign Guard", "RustPluginGenerator", "1.0.0")]
[Description("Restricts sleeping bag friend assignment to the acting player's team by default.")]
public class BedAssignGuard : RustPlugin
{
private const string PermBypass = "bedassignguard.bypass";
private PluginConfig config;
private class PluginConfig
{
[JsonProperty("Only allow team members")]
public bool TeamOnly { 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>
{
["NotTeammate"] = "You can only assign this bed to a member of your team.",
["NoTeam"] = "Join a team before sharing a sleeping bag."
}, 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 CanAssignBed(BasePlayer player, SleepingBag sleepingBag, ulong targetUserID)
{
if (!config.TeamOnly || player == null || sleepingBag == null)
return null;
if (permission.UserHasPermission(player.UserIDString, PermBypass))
return null;
if (player.currentTeam == 0)
{
player.ChatMessage(Msg("NoTeam", player.UserIDString));
return true;
}
var team = RelationshipManager.ServerInstance.FindTeam(player.currentTeam);
if (team == null || !team.members.Contains(targetUserID))
{
player.ChatMessage(Msg("NotTeammate", player.UserIDString));
return true;
}
return null;
}
}
}The plugin blocks the friend-assignment action unless the target user ID belongs to the assigning player's current team. In CanAssignBed it first ignores players toggled to bypass mode by an admin permission, then compares the requested targetUserID with the player's team roster via RelationshipManager.ServerInstance.FindTeam. When the target is not on the team (or the player has no team), the hook returns true so the bag keeps its owner, and the interacting player gets a localized message. Permissions are registered at startup and the messages live in the language file.
Common use cases
Team-only bed sharing
Block reassigning a sleeping bag to anyone outside the acting player's current team, preventing cross-team respawn shortcuts.
VIP list for bed shares
Only allow the assignment when the target holds a permission, so server staff or donors can hand out bags selectively.
Log suspicious reassignments
Record who tried to assign which bag to which user ID in a data file for later review without blocking the action.
Pitfalls
Do not return false to allow
The hook is checked with
!= null, so any non-null value cancels the action. Returnnullto permit the assignment, neverfalse.Target may be offline or unknown
BasePlayer.FindByIDcan return null for a user ID that never joined, so validate the target before reading team or player state.Not triggered for initial placement
This hook only covers the friend-assign flow. Deploying a new bag or binding yourself uses different code paths and is not affected.
Plugin recipes that use CanAssignBed
Build a plugin with CanAssignBed
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 CanAssignBed
Generate