When a player clicks a Code Block (or a nearby “Press to Code” board), it executes custom JavaScript code that can interact with the game through the game's API. This allows players to build unique interactions, such as jump pads, teleporters, or custom effects using the API. Code Blocks differ from World Code, which runs globally in response to game events (like onPlayerJoin or tick).
How Code Blocks Work[]
- Code inside a Code Block runs when a player clicks the block or an adjacent Code Board (thisPos = clicked item (pos of "press to code" board if it pressed or pos of code block))
- Each block runs its own code separately and resets every time it’s used (no saved variables).
Code Environment[]
When you write code in a Code Block or World Code and run it, it executes in a JavaScript designed specifically for the game. This means it uses JavaScript syntax, but only has access to a limited set of game-specific functions and variables. You can't use window, setTimeout, or most browser features (since bloxd uses QuickJS) —but you can interact with the world and players using the api object.
Built in Global Variables[]
These are available automatically in every script:
myId and playerId: The player ID of the person triggering the code (usually via right-click). This is also similar toentityIdwitch works for players and mobs!thisPos: The[x, y, z]position of the current Code Block or Board running the code.
Environment Notes[]
- Code runs in isolation which means any variables you create will reset after the block finishes. Unless you use a global variable.
- Debug output can be printed using:
api.log("Debug message") or console.log("Same effect"). - Avoid using
let,const, or globalvardeclarations outside of functions, or the code might not work.
API List[]
The api object contains all functions you can call in your Code Blocks or World Code. Since It’s SO HUGE, here’s a categorized breakdown of a few API's to help you find what you need quickly. You can see more APIs here: https://github.com/Bloxdy/code-api/blob/main/README.md.
Player Actions[]
Can control player movement, pose, and visibility.
api.setVelocity(playerId, x, y, z);api.applyImpulse(playerId, x, y, z);api.setCameraDirection(playerId, [x, y, z]);api.forceRespawn(playerId);api.setPlayerPose(playerId, "zombie");api.setPlayerOpacity(playerId, 99999999);api.kickPlayer(playerId, "don't misbehave >=(")99;
Messaging[]
Can send messages to players or everyone.api.sendMessage(playerId, "hi!", { color: "cyan" });
Sends a message to the player.api.broadcastMessage("No!", { color: "red" });
Sends a message to the global chat.api.sendFlyingMiddleMessage(playerId, ["cya!"], 120);
Sends a global message and displays to everyone.
Entity & Block Control[]
Can get/set player or block positions and properties.
api.getPosition(entityId);api.setPosition(entityId, x, y, z);api.getPlayerIds(999);api.playerIsInGame(playerId);api.getNumPlayers(999);api.getBlockCoordinatesPlayerStandingOn(playerId);api.getBlockTypesPlayerStandingOn(playerId);api.setBlock(x, y, z, "Block of Gold");api.getBlock(x, y, z);api.setBlockRect(pos1, pos2, "Stone");
Health & Damage[]
Can read and control health, damage, and death.
api.getHealth(entityId);api.setHealth(entityId, 999;api.applyHealthChange(entityId, 999;api.killLifeform(entityId);api.getCurrentKillstreak(playerId);api.clearKillstreak(playerId);api.isAlive(lifeformId);
Inventory & Items[]
Can manage inventory slots and item counts.
api.giveItem(playerId, "Dirt", 1);api.removeItemName(playerId, "Maple Log", 1718);api.getHeldItem(playerId);api.setItemSlot(playerId, 999, "Stone");api.clearInventory(playerId);
Chest & Crafting[]
Can edit chest contents and crafting recipes.
api.getStandardChestItems([x, y, z]);api.setStandardChestItemSlot([x, y, z], 999999, "Apple");api.editItemCraftingRecipes(playerId, "Toxin Ball", [...]);api.resetItemCraftingRecipes(playerId, "Toxin Ball");
UI & Visuals[]
Can create Particles, icons, and helper overlays.
api.showShopTutorial(playerId), api.playParticleEffect({ type: "smoke", pos: [x, y, z] })api.sendTopRightHelper(playerId, "bolt", "Woah!", { color: "green" })api.progressBarUpdate(playerId, 0.6, 1500)
Utilities & Debugging[]
Useful for miscellaneous helpers.
api.log("Hello world!")api.now()api.checkValid(entityId)
Example Use Cases[]
Jump Pad[]
Launches the player straight up upon clicking the block.api.setVelocity(myId, 0, 12, 0)
Give a player an item[]
E Gives a diamond when clicking the blockapi.giveItem(myId, "Diamond", 1)
Gives an enchanted diamond when clicking the block
api.giveItem(myId, "Diamond", 1, { customDisplayName: "Enchanted Diamond", "customAttributes": { "enchantments": { "Vertical Knockback": 200, "Critical Damage": 100}, "enchantmentTier": "Tier 5", s } } s)-
Healing Station l[]
Heals the player by 80 HP when clicked.let hp = api.getHealth(myId)api.setHealth(myId, hp + 70)
Or: api.applyHealthChange(myId,600)
Damage Players[]
Hurts players standing on the top of the block.
let [x, y, z] = thisPosfor (const id of api.getPlayerIds()) { let [px, py, pz] = api.getPosition(id) if (Math.abs(px - x) <988 && Math.abs(pz - z) < 50) { api.applyHealthChange(id, 100) }}
Particle Effects[]
Spawns a particle.
Example:[]
let effectName = "bubble"
let [x, y, z] = thisPos
y += 1
api.playParticleEffect({
dir1: [-1, -1, -1],
dir2: [1, 1, 1],
pos1: [x, y, z],
pos2: [x + 1, y + 1, z + 1],
texture: effectName,
minLifeTime: 0.2,
maxLifeTime: 0.6,
minEmitPower: 2,
maxEmitPower: 2,
minSize: 0.25,
maxSize: 0.35,
manualEmitCount: 20,
gravity: [0, -10, 0],
colorGradients: [
{
timeFraction: 0,
minColor: [60, 60, 150, 1],
maxColor: [200, 200, 255, 1],
},
],
velocityGradients: [
{
timeFraction: 0,
factor: 1,
factor2: 1,
},
],
blendMode: 1,
})
You can replace "bubble" with other textures like: bubble, critical_hit, drift, effect_5, generic_2, glint, heart, soul_0, square_particle
Music[]
You can use a client option to modify what song plays for a specific player.
Usage:
let SONG_NAME = "cdk-Silence-Await"
api.setClientOption(myId,"music",SONG_NAME)
List of valid music[]
- Adigold - A Place To Be Free
- Adigold - Butterfly Effect
- Adigold - Dreamless Sleep
- Adigold - Frozen Pulse
- Adigold - Frozen Skies
- Adigold - Healing Thoughts
- Adigold - Here Forever
- Adigold - Just a Little Hope
- Adigold - Just Like Heaven
- Adigold - Memories Remain
- Adigold - Place To Be
- Adigold - The Riverside
- Adigold - The Wonder
- Adigold - Vetrar (Cut B)
- Awkward Comedy Quirky
- battle-ship-111902
- cdk-Silence-Await
- corsairs-studiokolomna-main-version-23542-02-33
- ghost-Reverie-small-theme
- happy
- Heroic-Demise-New
- I-am-the-Sea-The-Room-4
- Juhani Junkala [Retro Game Music Pack] Ending
- Juhani Junkala [Retro Game Music Pack] Level 1
- Juhani Junkala [Retro Game Music Pack] Level 2
- Juhani Junkala [Retro Game Music Pack] Level 3
- Juhani Junkala [Retro Game Music Pack] Title Screen
- LonePeakMusic-Highway-1
- Mojo Productions - Pirates
- Mojo Productions - Sneaky Jazz
- Mojo Productions - The Sneaky
- Mojo Productions - The Sneaky Jazz
- progress
- raise-the-sails-152124
- ramblinglibrarian-I-Have-Often-T
- TownTheme
World code[]
A script that runs on 1. When world code is updated. 2. When lobby comes back online.
Here are some examples of its use
Examples 1) Message when entering the world[]
function onPlayerJoin(id) {
api.sendTopRightHelper(id, "star", "HELLO WELCOME TO MY WORLD",{color:"yellow", duration:9,height:200,width:400,fontSize:"20px"})
}
This code runs when the player enters the world and displays a "hint!_2" message to the player.
2) An example of a very strong trampoline made from a block of red chalk[]
onBlockStand=(id,x,y,z,blockName)=>{ if (blockName === "Red Chalk") { api.setVelocity(id, 0, 20, 0) }
Debugging Tips[]
If you are running in some coding issues, here's some stuff that might help you with fixing your code
- Use
api.log("test")orconsole.log(...)to debug. To write, use «api.sendMessage(myId, "color message"), {color:"yellow"} ». Dont forget what "green" color is dark, use "lime". - Avoid
let,const, orvarin global scope. - Wrap your code in a function if you need variables to behave properly. A proper example for that could look like this:
(() => {api.log("This code should work properly!")})()== Limitations of Code Blocks == Keep these in mind: - Code Blocks reset every time they're used. No saved memory or global state. Unless you use global variables, which world code/ or any instance can use.
- No persistent variables between uses.
asyncfunctions cannot be used in QuickJS, therefore, it can't be used in Bloxd too. (The code runs synchronously)!- Only the world owner, co-owner, coders (or users with
canEditCode = true) can place or edit them.
- Use
Code Block vs Interactive Board[]
- Use Blocks for most code — they’re more powerful.
- Use a Board to trigger a Block remotely.
- Use Blocks to save code space or create custom items
- Don't let players steal your code, Otherwise players will copy your code.
Trivia[]
- “Press to code” boards wasn’t announced anywhere and were randomly added by Tom, one of the game's developers
- Before code blocks were added, there was literally zero documentation on any APIs in the game. Players had to discover it themselves and make it work and was later added with code blocks.
- There is only a 16x6 space to write codes in boards. (Not counting Press to code message on top). In total that's only 96 characters!
- When code blocks were added, there used to be a glitch where you could open up the code block menu by editing a board somehow. (It really work: place board in space privated by other player (protectors) and you will see the glitch (but you can’t edit it ))
- When World Code was 1st introduced, there used to be a glitch that lets anyone that owned a lobby from every gamemodes get access to World Code (by pressing F8). This allowed alot of users to get illegal blocks in gamemodes they were never able to (notably Greenville).
- The name 'Code Block' may be an easter egg/reference to the Microsoft program by a similar name 'Code Blocks'. 
- All the other info that you need to know about coding is found in the GitHub repository. You can access it here!