Bedrock Wiki
  • Discord
  • Contribute
  • bedrock.dev
  • MS Learn
Beginner's Guide
  • Guide
    • 1. Introduction
      guide
    • 2. Add-Ons Explained
    • 3. Software & Preparation
    • 4. Project Setup
    • 5. Create a Custom Item
    • 6. Create a Custom Entity
    • 7. Blockbench: Modeling, Texturing & Animating
    • 8. Adding a Loot Table, Spawn Rule & Crafting Recipe
  • Extra
    • a. Understanding JSON
    • b. Download Example Packs
    • c. Troubleshooting
      help
    • d. Advanced Manifest
    • e. Format Versions
    • f. Project Setup Android
Animation Controllers
  • Intro to Animation Controllers
    guide
  • Entity Commands
  • AFK Detector
  • Death Commands
  • Molang into Scoreboard
  • Respawn Commands
Blocks
  • General
    • Intro to Blocks
      guide
    • Block Components
    • Block Tags
    • Block States
    • Block Traits
    • Block Permutations
    • Block Events
      Scripts
    • Block Event Migration
      help
    • Blocks as Items
    • Troubleshooting Blocks
      help
  • Visuals
    • Block Culling
    • Block Models
      guide
    • Block Texture Animation
    • Block Texture Variation
    • Block Tinting
  • Tutorials
    • Applying Effects
      Scripts
    • Avoiding State Limit
    • Fake Blocks
      Scripts
    • Ore Loot Tables
      Scripts
    • Precise Rotation
      Scripts
    • Rotatable Blocks
  • Vanilla Re-Creations
    • Custom Crops
      Scripts
    • Custom Glass
    • Custom Glazed Terracotta
    • Custom Trapdoors
      Scripts
  • Documentation
    • Block Format History
    • Block Shapes
    • Block Sounds
    • Vanilla Block Models
Commands
  • General
    • Intro to Command Blocks
    • Functions
    • Block States
    • Coordinate System
    • Giving NBT Items
    • Scoreboard Operations
    • Understanding Selectors
  • Commands
    • Damage
    • Execute
    • Playanimation
    • Playsound
  • On Event Systems
    • On First World Load
    • On Player First Join
    • On Player Join
    • On Player Leave
    • On Player Death
    • On Player Respawn
  • Scoreboard Systems
    • Entity Counter
    • Scoreboard Timers
    • Comparing And Retrieving Scores
  • Techniques
    • Execute Logic Gates
    • Execute at Block Variant
    • MBE - Max's Block Entity
    • FMBE - A New Way to Create Display Entities
    • Item Drop Detection
    • Look Detection
    • Movement Detections
    • Orbital Camera
  • Useful Creations
    • Compass Display
    • Custom Crafter
    • Multiplayer Position Rearrangement
      function
Concepts
  • contents.json
  • Molang
  • Namespaces
  • Overwriting Assets
  • Shaders
  • Sounds
  • Subpacks
  • Texture Atlases
  • textures_list.json
Documentation
  • Advanced Molang
  • File Types
  • Fog IDs
  • Material Configuration Description
  • Menu Categories
  • Molang Queries
  • Pack Folder Structure
  • Shared Constructs
  • Sound Definitions
  • Vanilla Materials
Entities
  • General
    • Intro to Entities BP
      guide
    • Intro to Entities RP
      guide
    • Troubleshooting Entities
      help
    • Entity Events
    • Entity Properties
    • NPC Dialogue
    • Render Controllers
    • Spawn Rules
  • Tutorials
    • Convert Points Between Any Space (World, Entity, Bones)
    • Creating Boats
    • Detecting Other Entities
    • Disabling Team Damage
    • Dummy Entities
    • Entity Attacks
    • Entity Holds Item
    • Entity Movement
    • Entity Timers
    • Flying Entities
    • Introduction to AOE Clouds
    • Invulnerable Entities
    • Look at Entity
    • Sleeping Entities
    • Solid Entities
    • Spawning Tamed Entities
      Scripts
    • Village Mechanic
  • Documentation
    • Dummy Components
    • Non-Mob Runtime Identifiers
    • Projectiles
    • Runtime Identifiers
    • Vanilla Usage Components
    • Vanilla Usage Spawn Rules
Items
  • General
    • Intro to Items
      guide
    • Item Components
    • Item Tags
    • Item Events
      Scripts
    • Item Event Migration
      help
    • Troubleshooting Items
      help
  • Tutorials
    • Custom Armor
    • Custom Food
      Scripts
    • Custom Pottery Sherds
    • Custom Weapons
    • Equipment-Based Commands
    • High Resolution Items
    • Spawning Items
    • Throwable Items
  • Documentation
    • Enchantments
    • Attachables
    • Item Format History
    • Numerical Item IDs
    • Vanilla Usage Components
JSON UI
  • General
    • Intro to JSON UI
      guide
    • Best Practices
      guide
  • Tutorials
    • Adding HUD Elements
    • Aseprite Animations
    • Buttons and Toggles
    • Modifying Server Forms
    • Preserve Title Texts
    • String to Number
  • Documentation
    • JSON UI Documentation
Loot, Recipes & Trading
  • General
    • Trading Behavior
  • Documentation
    • Loot Tables
    • Trade Tables
    • Recipes
    • Item Functions
  • Tutorials
    • Randomized Structure Loot
Meta
  • Add-On Performance
  • Style Guide
  • Useful Links
  • Using Schemas
  • Version Control
  • Q&A
    • Blocks and Items Q&A 2024/08/30
    • Deferred Technical Preview Q&A 2024/02/23
    • GameTest Q&A 2021/08/06
    • Scripting and Editor Q&A 2023/09/22
    • World Generation Q&A 2024/11/15
NBT
  • General
    • .mcstructure
  • Tutorials
    • Experiments in Minecraft Education
    • Extending Structure Limits
  • NBT in Depth
    • About NBT (Named Binary Tag)
    • NBT Libraries
    • Reading NBT Example
Particles
  • General
    • Intro to Particles
      guide
  • Tutorials
    • Disabling Particles
  • Documentation
    • Vanilla Particles
Scripting
  • General
    • Intro to Scripting
    • What is Script API?
    • API Modules
  • Tutorials
    • Block Placement Prevention
    • Custom Commands
    • GameTests
    • Script Core Features
    • Script Forms
    • Script Privileges
    • Script Requests API
  • Documentation
    • Engine Environment
    • Script Resources
    • Script Watchdog
    • Troubleshooting JavaScript
    • TypeScript
Servers
  • Software
    • Bedrock Server Software
  • Protocols
    • Bedrock Protocol
    • NetherNet Protocol
    • RakNet Protocol
Text & Localization
  • General
    • Intro to Text & Localization
    • Raw Text
    • Splashes
  • Emojis & Symbols
    • Intro to Emojis
    • Custom Emojis
    • Input Keys
Visuals
  • General
    • Introduction to Entity Visuals
      guide
    • Bedrock Modeling
    • Custom Death Animations
    • Effects in Animations
    • Material Creations
    • Materials
    • Math-Based Animations
    • Skin Packs
  • Tutorials
    • Entity Texture Animation
    • Glowing Entity Texture
    • Hurt Animations
    • Leash Position
    • Player Geometry
    • Remove Entity Shadows
    • Retexturing Spawn Eggs
  • Ideas
    • Structure Presentation
World Generation
  • General
    • Intro to World Generation
      guide
    • Biomes
      guide
    • Feature Types
    • Jigsaw Structures
  • Tutorials
    • Block Conditions for Features
    • Generating Custom Ores
    • Generating Custom Structures
    • Generating Patches
    • Heightmap Noise
  • Documentation
    • Biome Tags

Script Privileges

beginner
Script Privileges
  • Read-Only Mode
    • Escaping Read-Only Mode
  • Early-Execution Mode
    • Escaping Early-Execution Mode

This is a guide on understanding what Script API privileges are. This includes going through what read-only mode, and the new early-execution mode implemented in Scripting v2.0.0 are about.

You might have encounted this error when running your scripts, about not having required privileges when calling a native function or a property such as the following mesasge:

ReferenceError: Native function [Class::method] does not have required privileges.

There are two reasons this message occurs, one is that the method or property is called during read-only mode, or early-execution mode.

Read-Only Mode ​

This means scripts that are in this state are not allowed to alter world state within these callbacks. The script is in read-only state when Minecraft simulation begins and before events are triggered, or before the start of the "script" tick.

When the error above occurs, that means you attempted to run a function that tries to modify the state of the world on before events.

Escaping Read-Only Mode ​

The easiest way to resolve this issue is to move code, which contains all native functions that can't be called in read-only mode, and all native properties that can't be edited in read-only mode into a system.run callback. Here is an example scnerario where we're trying to modify the state of the world in a before event callback.

The native function MessageFormData.show() cannot be called in read-only mode, so the following code will throw a ReferenceError explaining that function does not have required privileges.

javascript
import { system, world } from "@minecraft/server";
import { MessageFormData } from "@minecraft/server-ui";

// Subscribe to playerInteractWithBlock event to detect
// if a player interacts with a block
world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
  // Check if player interacts with a crafter whilst holding diamonds
  if (
    event.block.typeId === "minecraft:crafter" &&
    event.itemStack &&
    event.itemStack.typeId === "minecraft:diamond"
  ) {
    // Cancel interaction
    event.cancel = true;

    const form = new MessageFormData()
      .title("Crafter")
      .body("This is a crafter!")
      .button1("Close");

    // Error throws as .show() requires privileges.
    form.show(event.player);

    // player.sendMessage is not limited by read-only mode,
    // so this function can be called here.
    player.sendMessage("Player is using the crafter.");
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

To adapt the code so it runs properly in read-only mode, the code above is changed into this:

javascript
import { system, world } from "@minecraft/server";
import { MessageFormData } from "@minecraft/server-ui";

// Subscribe to playerInteractWithBlock event to detect
// if a player interacts with a block
world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
  // Check if player interacts with a crafter whilst holding diamonds
  if (
    event.block.typeId === "minecraft:crafter" &&
    event.itemStack &&
    event.itemStack.typeId === "minecraft:diamond"
  ) {
    // Cancel interaction
    event.cancel = true;

    // Creating a new MessageFormData object, calling title,
    // body, and button1 can be called within read-only mode.
    // This means they can be called within this
    // callback before script tick.
    const form = new MessageFormData()
      .title("Crafter")
      .body("This is a crafter!")
      .button1("Close");

    // If event.player is used during script tick, declare
    // it in a variable, otherwise you can't retrieve the
    // player object, as event object turns into null
    // after read-only state ends.
    const player = event.player;

    // Use system.run to queue for later in the current
    // tick to bypass read-only state
    system.run(() => {
      // Show the player a message form in script tick.
      // As MessageFormData.show() is marked so it can't
      // be called in read-only mode
      form.show(player);
    });

    // player.sendMessage is not limited by read-only mode,
    // so this function can be called here.
    player.sendMessage("Player is using the crafter.");
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

Difference

diff
-    form.show(event.player);
+    // If event.player is used during script tick, declare
+    // it in a variable, otherwise you can't retrieve the
+    // player object, as event object turns into null
+    // after read-only state ends.
+    const player = event.player;

+    // Use system.run to queue for later in the current
+    // tick to bypass read-only state
+    system.run(() => {
+      // Show the player a message form in script tick.
+      // As MessageFormData.show() is marked so it can't
+      // be called in read-only mode
+      form.show(player);
+    });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

We highly recommend that you check the API reference to see if the method or property can be called in read-only state. Each property or method that can't be called in read-only mode is mentioned in the description with the following text:

  • This function can't be called in read-only mode.
  • This property can't be edited in read-only mode.

Early-Execution Mode ​

This means the script is an environment before the world is loaded, which means most APIs like general world gamemode get property queries, are not ready to be accessed and worked with.

When the privilege error occurs, that means you attempted to run a function or edit a property that requires the world is loaded, before the world is loaded.

Escaping Early-Execution Mode ​

If you have code using an API that is being run in the root context of a script file, it will need to be deferred to run either during or after the world.afterEvents.worldLoad event, or the traditional system.run timed callback.

You also need to move code that uses event the APIs below, if they were run in the root context of a script file previously, to root context instead of the world.afterEvents.worldLoad event, to avoid some events not being triggered, leading to script malfunction.

The following is an example of sending a message when script loads. Before v2.0.0 breaking changes, the script used to look like this:

javascript
import { world } from "@minecraft/server";

// This procedure is placed in root context of a script file.
for (const player of world.getAllPlayers()) {
  player.sendMessage(`Hello ${player.name}!`);
}

world.afterEvents.playerSpawn.subscribe(({ player, initialSpawn }) => {
  if (!initialSpawn) return;
  player.sendMessage(`Hello ${player.name}!`);
});
1
2
3
4
5
6
7
8
9
10
11

To adapt to scripting v2.0.0 changes, the code above is changed into this:

javascript
import { world } from "@minecraft/server";

// Keep the event subscription function that was in root
// context of a script file previously there.
world.afterEvents.playerSpawn.subscribe(({ player, initialSpawn }) => {
  if (!initialSpawn) return;
  player.sendMessage(`Hello ${player.name}!`);
});

world.afterEvents.worldLoad.subscribe(() => {
  // This procedure is moved inside the worldLoad callback,
  // since world.getAllPlayers() can't be called in
  // early-execution mode.
  for (const player of world.getAllPlayers()) {
    player.sendMessage(`Hello ${player.name}!`);
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Difference

diff
- for (const player of world.getAllPlayers()) {
-   player.sendMessage(`Hello ${player.name}!`);
- }
+ world.afterEvents.worldLoad.subscribe(() => {
+   // This procedure is moved inside the worldLoad callback,
+   // since world.getAllPlayers() can't be called in
+   // early-execution mode.
+   for (const player of world.getAllPlayers()) {
+     player.sendMessage(`Hello ${player.name}!`);
+   }
+ });
1
2
3
4
5
6
7
8
9
10
11

Early-Execution APIs ​

The following are the initial APIs available in early execution mode for scripting v2.0.0-beta:

  • world.beforeEvents.*.subscribe
  • world.beforeEvents.*.unsubscribe
  • world.afterEvents.*.subscribe
  • world.afterEvents.*.unsubscribe
  • system.afterEvents.*.subscribe
  • system.afterEvents.*.unsubscribe
  • system.beforeEvents.*.subscribe
  • system.beforeEvents.*.unsubscribe
  • system.clearJob
  • system.clearRun
  • system.run
  • system.runInterval
  • system.runJob
  • system.runTimeout
  • system.waitTicks
  • BlockComponentRegistry.registerCustomComponent
  • ItemComponentRegistry.registerCustomComponent

Again, please check the API reference to see if the method or property can be called in early-execution state. Each property or method that can be called in early-execution mode is mentioned in the description with one of the following text:

  • This property can be read in early-execution mode.
  • This function can be called in early-execution mode.

Contributors

Edit Script Privileges on GitHub

Text and image content on this page is licensed under the Creative Commons Attribution 4.0 International License

Code samples on this page are licensed under the MIT License

Bedrock Wiki by Bedrock OSS

"Minecraft" is a trademark of Mojang AB.

Bedrock OSS, Bedrock Wiki and bedrock.dev are not affiliated in any way with Microsoft or Mojang AB.

  • Privacy Policy
  • Join our Discord
  • Learn how to Contribute
  • Visit our Repository