OnTeamLeave
OnTeamLeave is called when a player leaves their current team, before the player is removed from the team and their team reference is cleared. Returning a non-null value blocks the leave.
object OnTeamLeave(RelationshipManager.PlayerTeam playerTeam, BasePlayer player)Parameters
| Parameter | Type | Description |
|---|---|---|
playerTeam | RelationshipManager.PlayerTeam | The RelationshipManager.PlayerTeam the player is currently on. Read its members and teamLeader lists to report or evaluate the team before the player is removed. |
player | BasePlayer | The BasePlayer who is trying to leave the team. Use player.userID or player.UserIDString for identity and player.currentTeam for the team id. |
Return value
Return null (or nothing) to let the leave proceed: the player is removed from the team and their team reference is cleared. Return any non-null value to cancel the leave, so the player stays on the team exactly as before.
How it works
This hook fires inside RelationshipManager.leaveteam, which is the code path used when a player leaves a team (for example through the team UI). At the call site the player already has a team: the game resolves player.currentTeam, looks up the team object, and only then calls the hook. Both playerTeam and player are useful here: playerTeam.members and playerTeam.teamLeader describe the team as it stands before the change, and player identifies who is leaving. Because the hook is declared with the exact RelationshipManager.PlayerTeam and BasePlayer types, Oxide dispatches it for every leave attempt; there is no broader type to narrow here. Practical uses are gating the leave (admins keeping members on a team, cooldown or penalty systems) and taking a snapshot of team state before it changes. If you block the call, the player stays on the team and ClearTeam() is never reached. Note that the team lookup uses the player's current team id, so stale or already-cleared teams never reach this hook.
Example plugin: Prevent owners from abandoning a team while it still has members
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Team Owner Leave Guard", "RustPluginGenerator", "1.0.0")]
[Description("Prevents a team leader from leaving while other members are still on the team.")]
public class TeamOwnerLeaveGuard : RustPlugin
{
private const string PermBypass = "teamownerleaveguard.bypass";
private PluginConfig config;
private class PluginConfig
{
[JsonProperty("Bypass permission name")]
public string BypassPermission = PermBypass;
}
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>
{
["LeaveBlocked"] = "You are the team leader. Kick the remaining members or disband the team before leaving."
}, this);
}
private string Msg(string key, string userId = null) => lang.GetMessage(key, this, userId);
private void Init()
{
permission.RegisterPermission(PermBypass, this);
if (!string.IsNullOrEmpty(config.BypassPermission) && config.BypassPermission != PermBypass)
permission.RegisterPermission(config.BypassPermission, this);
}
private string ResolvePerm(string userId)
{
if (!string.IsNullOrEmpty(config.BypassPermission) && permission.UserHasPermission(userId, config.BypassPermission))
return config.BypassPermission;
return permission.UserHasPermission(userId, PermBypass) ? PermBypass : null;
}
private object OnTeamLeave(RelationshipManager.PlayerTeam playerTeam, BasePlayer player)
{
if (playerTeam == null || player == null) return null;
if (ResolvePerm(player.UserIDString) != null) return null;
// Leader may leave only when no other members remain.
if (playerTeam.teamLeader != player.userID.Get()) return null;
if (playerTeam.members == null || playerTeam.members.Count <= 1) return null;
player.ChatMessage(Msg("LeaveBlocked", player.UserIDString));
return true;
}
}
}The plugin blocks a team leader from leaving a team that still has other members, so a group cannot be left leaderless by accident. In OnTeamLeave it null-checks both parameters, finds the configurable bypass permission and checks it against the leaving player. If the player is the team leader and playerTeam.members.Count is greater than one, it sends the configured chat message and returns a non-null value to cancel the leave. If the guard does not apply it returns null and the game removes the player normally. All player-facing text comes from the language file so the message can be translated, and the config exposes the permission name.
Common use cases
Leader leave protection
Stop a team leader from leaving while other members remain, forcing them to kick members or disband the team first.
Leave cooldown or penalty
Return a non-null value when the player left the team recently, using stored timestamps, and show a cooldown message instead of allowing an instant leave.
Audit team departures
Snapshot the team leader and member list when the hook fires and store the record in oxide/data before the player is removed from the team.
Pitfalls
Blocking also affects admins and other systems
Anything that calls RelationshipManager.leaveteam goes through this hook, including admin commands and other plugins invoking the method. Gate your block behind a permission or an explicit condition.
The team is not modified yet
The player is still in playerTeam.members when the hook runs. Do not send a 'you left' confirmation here, because the leave may still be cancelled by another plugin or a non-null return.
No hook on direct server-driven wipes
When a team is disbanded or the server clears teams through other paths, this leave code may not run. Handle OnTeamDisband or OnTeamDisbanded if you need to react to those cases.
Plugin recipes that use OnTeamLeave
- How to make a Rust team auto authorization plugin (TC, locks, turrets)Automatically authorizes a player on every tool cupboard, code lock and auto turret owned by their team when they join a team, and removes that authorization when they leave, are kicked or the team disbands. It uses OnTeamAcceptInvite, OnTeamLeave, OnTeamKick and OnTeamDisbanded.
- How to make a Rust team switch cooldown plugin (leave/kick lockout)Blocks a player from joining a team for N hours after leaving or being kicked, using OnTeamLeave, OnTeamKick and OnTeamAcceptInvite with per-player UTC timestamps saved in oxide/data.
Build a plugin with OnTeamLeave
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 OnTeamLeave
Generate