Skip to content

Lua Scripts

MissingTextureMeow101 edited this page Mar 4, 2026 · 11 revisions

In the editor, you can select "Custom Challenge", which allows you to write lua scripts to make basic custom challenges.

Disclaimer: The Lua scripting system is designed for creating custom challenge maps, not for custom items/npcs/etc or making Hide and Seek maps. (So don't ask for Happy Baldi and stuff.)

Lua scripts have some optional functions, and some required ones. You MUST have the required functions in your script or you will get errors.

Note that some of the required functions may not have globals defined yet, which means trying to call any self function or any function listed here will not work and throw an error.

Required Functions

  • Initialize()
  • SetupPlayerProperties()
  • ExitedSpawn()
  • Update(delta)
  • AllNotebooks()

Initialize()

Called when the level is initialized. This is the first method that is called where all globals have been defined.

SetupPlayerProperties()

Called before initialized, globals are still undefined. The table returned is used to modify the player's movement variables. If you don't want to change a value from the default, don't include it in the table.

Below is an example function that has all the values at their default.

function SetupPlayerProperties() return { walkSpeed=16, runSpeed=24, staminaDrop=10, staminaMax=100, staminaRise=20 } end

ExitedSpawn()

This is called when the player leaves the elevator. It is recommended you call self:SpawnNPCs() and self:StartEventTimers() here, but you do not have to.

Update(delta)

This is called every update frame/tick. Delta is the delta time, which is the time that passed inbetween this update and the last one. You can use it to make timers.

AllNotebooks()

This is called when the player has collected all notebooks. For most challenges, you want to call self:OpenExits(true) here.

Optional Functions

  • NotebookCollected(notebookPosition)
  • AngerBaldi(value)
  • AllNPCsSpawned()
  • OnItemUse(itemId, slot)
  • GiveRandomSticker(packTypeId, total)
  • OnLevelCompleted()
  • NoiseMade(position, value)
  • BaldiSlapped(baldi)
  • OnActivityProgress(room)
  • OnActivityCompletion(room, correct)

NotebookCollected(notebookPosition)

This is called whenever a notebook gets collected. notebookPosition is the notebook's position in world space.

AngerBaldi(value)

This is called whenever the game wants to make Baldi angry. The return value of this function can be used to change how angry Baldi gets. Below is an example function that makes Baldi get twice as angry.

function AngerBaldi(value) return value * 2 end

AllNPCsSpawned()

BB+ only spawns NPCs if the player isn't too close. This function is called when every single NPC has spawned in for the first time. This is not called if SpawnNPCs is called multiple times.

OnItemUse(itemId, slot)

Called when the player attempts to use an item. The return value determines if the item can be used. Returning true allows the item to be used while returning false prevents it from being used.

OnGiveRandomSticker(packTypeId, total)

Called when the game attempts to give the player a random sticker of the specified pack type. (Refer to the cheat sheet). Total is the amount of stickers that are being given. Does not get called when self:GiveRandomSticker is called. Returning false cancels the default behavior, which can be used to alter the sticker given or amount of stickers using self:GiveRandomSticker.

OnLevelCompleted()

Called when the level is about to be completed, returning a string will change the ending message.

NoiseMade(position, value)

Called whenever a noise is made. position is a Vector3, and value is the noise value.

BaldiSlapped(baldi)

Called whenever Baldi slaps. baldi is the Baldi that slapped.

OnActivityProgress(room)

Called whenever progress is made on an activity. room is the room with the activity.

OnActivityCompletion(room, correct)

Called when an activity is completed. correct is whether or not it was done correctly, room is the room with the activity.

Global Functions And Variables

Global Variables:

  • player - The current Player.

Global Functions:

  • Vector3(x,y,z) - Creates a new Vector3 with the specified x, y, and z coordinates.
  • IntVector2(x,z) - Creates a new IntVector2 with the specified x and z coordinates.
  • Color(r,g,b) - Creates a new Color with the specified r, g, and b values. (0-255)
  • RandomDecimalNumber(min, max) - Generates a random decimal number between min and max, unlike math.random which can only generate integers.

Self Functions And Variables

You may have seen the "self" variable being used. This refers to BB+'s game manager, which is what your script is managing. You cannot add custom variables to self. (You can define variables in the global scope/outside a function if you want custom variables)

SpawnNPCs()

Spawns all the NPCs that were placed in the editor.

StartEventTimers()

Starts the events and the level timer. This can only be called once.

GetBaldi()

This gets the current Baldi. If there are multiple it returns the first one it finds, which is usually the first one spawned. This can return nil if Baldi hasn't been spawned yet.

StartEvent(eventId, time, doJingle)

This starts the event with the specified event id, with the id being the id used in the level loader. The time is how long the event lasts, if it is 0 or below, the time will be randomly chosen in the same way as the regular random events. If doJingle is true, the usual event fanfare will occur with Baldi announcing it on the TV. Otherwise, the event will just happen instantly.

GetNPCTimeScale()

Returns the current NPC timescale.

GetEnvironmentTimeScale()

Returns the current Environment timescale.

GetPlayerTimeScale()

Returns the current Player timescale.

OpenExits(doEscape)

This opens all the exits, making completing the level possible. If doEscape is true, the standard exit sequence will play out. If it is false, all exits can be entered like in the tutorial. Sets escapeSequenceActive to true.

GetRandomEntitySafeCell()

This returns a random cell that is safe for entites to be on.

CellFromPosition(position)

Position can be either a Vector3 or an IntVector2. This gets the cell at the corresponding position.

GetAllRooms()

Returns a table of all the rooms in the map.

GetAllLights()

Returns a table of all lights on the map.

MakeNoise(position, noiseValue)

This makes noise that Baldi and others can hear at the specified position and noise value.

GetNPC(npcId)

This gets the first NPC with the specified the ID. The ID being the ID for the NPC in the level loader.

SpawnNPC(npcId, position)

This spawns and returns the NPC at the specified Vector3 position. The ID being the ID for the NPC in the level loader.

GetNPCs()

This gets all NPCs on the map and returns them in a table.

ActivateBonusProblems(includeLast)

This makes all activities activate their bonus problems. If includeLast is true, this will also activate bonus problems on the most recently completed machine.

ForceLose()

This forcefully ends the game, showing the Baldi jumpscare animation.

ForceWin(text)

This causes the level to be instantly completed with the specified text. If text is not passed in, the default win message is used.

GiveRandomSticker(packTypeId, total)

Give a number of stickers determined by total, from the specified sticker pack (packTypeId, refer to cheat sheet).

SpawnItemPickup(position, itemId)

Spawns an item with itemId at the specified Vector3 position. Does not work correctly with sticker packs.

PlaySoundObject(soundObjectName)

Plays the specified vanilla SoundObject globally.

notebookAngerVal

This variable is the amount that Baldi will get angry by when you collect a notebook. If you collect a notebook before Baldi is spawned, this value will get changed.

notebookCount

This gets the currently collected amount of notebooks.

totalNotebooks

This gets the total amount of notebooks on the map.

npcTimeScaleMod

This variable is the current npc timescale modifier. (not the current npc timescale) Changing this value will speed up or slow down the speed of npcs.

environmentTimeScaleMod

This variable is the current environment timescale modifier. (not the current environment timescale) Changing this value will speed up or slow down the speed of the environment.

playerTimeScaleMod

This variable is the current player timescale modifier. (not the current player timescale) Changing this value will speed up or slow down the speed of the player.

escapeSequenceActive

This variable determines if the escape sequence is active. In this context, it means whether or not elevators should start going Out Of Order. Automatically set to true when OpenExits is called.

elevatorManager

Refers to the elevator manager. Refer to it's dedicated section.

NPC Functions and Variables

NPC Functions:

  • AddArrow(r,g,b)
  • Squish(time)
  • Unsquish()
  • IsHidden()
  • GetForward()

AddArrow(r,g,b)

This adds an arrow onto the map that tracks this NPC with a color of the specified R G and B values. (0-255)

Squish(time)

Squishes the NPC for the specified amount of time.

Unsquish()

Unsquishes the NPC.

IsHidden()

Returns true if the NPC is hidden.

GetForward()

Returns the NPC's forward vector.

NPC Variables:

  • moveSpeedMultiplier
  • position
  • id
  • squished
  • direction
  • objectName

moveSpeedMultiplier

This variable is a multiplier to the NPC's movement speed, higher values make the NPC faster while lower values make the NPC slower.

id

This is the variable that stores the NPC's id.

position

This is the NPCs current position. Changing this will teleport the NPC to the specified coordinates.

squished

Returns if the NPC is currently squished. If it is set to true, the NPC will be squished for an infinite amount of time. Calling Unsquish or setting squished to false would be required to unsquish.

direction

The direction the NPC is facing/its rotation on the y axis.

objectName

The name of the NPC's GameObject. Useful for identifying NPCs even if id is "unknown." Can be set.

Baldi Functions And Variables

Baldi shares all the functions and variables with NPCs. But he also has the additional functions:

  • AddAnger(amount)
  • SetAnger(amount)
  • Praise(time)
  • Slap()

AddAnger(amount)

This increases Baldi's anger by the specified amount. This does not call AngerBaldi.

SetAnger(amount)

This sets Baldi's anger to the specified amount. This does not call AngerBaldi.

Praise

This makes Baldi congratulate/praise the player for the specified amount of time.

Slap()

Forces Baldi to slap his ruler.

Player Functions And Variables

The player refers to well, the player. Items are referenced by id, which is the id of the item in the level loader. Slot indexes start with 1 being the first slot, 2 being the second slot, etc.

Variables:

  • position
  • slotCount
  • points
  • moveSpeedMultiplier
  • stamina
  • staminaNormal
  • baseWalkSpeed
  • baseRunSpeed
  • baseStaminaMax
  • baseStaminaDrop
  • baseStaminaRise
  • walkSpeed
  • runSpeed
  • staminaMax
  • staminaDrop
  • staminaRise
  • squished
  • direction

position

The player's current position, setting this will teleport the player.

slotCount

The player's current inventory size. Setting this calls SetSlotCount.

points

The player's current amount of YTPs. Setting this will show the YTP incrementing/decrementing animation.

moveSpeedMultiplier

This variable is a multiplier to the player's movement speed, higher values make the player faster while lower values make the player slower.

stamina

This variable is the player's current stamina.

staminaNormal

This variable is the player's current stamina divided by max stamina, where 1 is always the current max stamina. Can be set as well.

walkSpeed

The player's walk speed. Cannot be set.

runSpeed

The player's run speed. Cannot be set.

staminaMax

The max amount of stamina the player can naturally regenerate. Cannot be set.

staminaDrop

The amount stamina will be reduced by when running. Cannot be set.

staminaRise

The amount stamina will increase by when resting. Cannot be set.

baseWalkSpeed

The player's base walk speed. Can be set.

baseRunSpeed

The player's base run speed. Can be set.

baseStaminaMax

The base max amount of stamina the player can naturally regenerate. Can be set.

baseStaminaDrop

The base amount stamina will be reduced by when running. Can be set.

baseStaminaRise

The base amount stamina will increase by when resting. Can be set.

squished

Returns if the player is currently squished. If it is set to true, the player will be squished for an infinite amount of time. Calling Unsquish or setting squished to false would be required to unsquish.

direction

The direction the player is facing/their rotation on the y axis.

Functions:

  • SetItem(itemId, slot)
  • AddItem(itemId)
  • GetItem(slot)
  • RemoveItemSlot(slot)
  • SetSlotCount(count)
  • LockItemSlot(slot)
  • UnlockItemSlot(slot)
  • UseItem(itemId)
  • HasItem(itemId)
  • RemoveItemOfID(itemId)
  • RemoveItem(slot)
  • MakeGuilty(ruleBreak, time)
  • GetGuilt()
  • Squish(time)
  • Unsquish()
  • IsHidden()
  • GetForward()
  • AddStickerToInventory(stickerId, animation)
  • GetInventoryStickers()
  • GetActiveStickers()
  • GetInventorySticker(slot)
  • GetActiveSticker(slot)
  • GetStickerValue(stickerId)
  • ApplySticker(slot, stickerId)

SetItem(itemId, slot)

This forcefully sets the item in the specified slot to be the item with the specified id.

AddItem(itemId)

This gives the item with the specified id to the player.

GetItem(slot)

This returns the id of the item at the specified slot, "unknown" if it isn't an item included in the loader.

RemoveItemSlot(slot)

This removes the specified item slot from the inventory, reducing the total number of slots. This is the function used in the limited items challenge.

SetSlotCount(count)

This sets the player's inventory to the specified amount of slots.

LockItemSlot(slot)

Locks the specified slot, preventing characters like Bully from stealing it.

UnlockItemSlot(slot)

Unlocks the specified slot, allowing characters like Bully from stealing it.

UseItem(itemId)

Simulates the player using the specified item. The player does not need to have the item in the inventory or have any slots available for this to work.

HasItem(itemId)

Returns true if the player has the item of the specified type.

RemoveItemOfID(itemId)

Removes the first item found if it has the matching ID.

RemoveItem(slot)

Removes the item at the specified slot.

MakeGuilty(ruleBreak, time)

Makes the player guilty of violating the specified rule for the specified time. Refer to the cheat sheet for valid vanilla rule breaks.

GetGuilt()

Returns the player's current guilt. Refer to the cheat sheet for vanilla rule breaks.

Squish(time)

Squishes the player for the specified amount of time.

Unsquish()

Unsquishes the player.

IsHidden()

Returns true if the player is hidden.

GetForward()

Returns the player's forward vector.

AddStickerToInventory(stickerId, animation)

Adds the specified sticker into the player's sticker inventory. animation is a bool that determines if the sticker opening animation plays.

GetInventoryStickers()

Returns an array of the sticker ids in the player's inventory

GetActiveStickers()

Returns an array of the sticker ids that are active.

GetInventorySticker(slot)

Returns the sticker at that slot in the sticker inventory

GetActiveSticker(slot)

Returns the sticker at that slot in the active stickers list.

GetStickerValue(stickerId)

Returns the amount of the sticker with the specified id the player has active.

ApplySticker(slot, stickerId)

Sets the active slot to contain a sticker of the specified sticker id.

Cell Functions and Variables

Cells, or also commonly referred to as tiles, are something you are probably already familiar with.

Variables:

  • position - The cells grid position as an IntVector2.
  • FloorWorldPosition - The Vector3 corresponding to the floor of the cell.
  • CenterWorldPosition - The Vector3 corresponding to the center of the cell.

Functions:

  • GetLight()
  • GetRoom()
  • SetLight(color, strength)
  • PressAllButtons()

GetLight()

Returns the light for the cell, if it exists. Otherwise, returns nil.

GetRoom()

Returns the room for the cell.

SetLight(color, strength)

Creates or modifies the light at the cell, then returns the newly added or modified light. Can return nil.

PressAllButtons()

Finds all buttons/levers/valves placed on that cell and attempts to press them.

Light Functions and Variables

A light lights up an area, determined by strength, with the color.

Variables:

  • cell - The cell this light belongs to.
  • color - The color of the light. Changing this will update the light.
  • strength - The strength of the light. Changing this will update the light.

Functions:

  • SetPower()

SetPower(on)

Sets the power to the specified state. Internally Plus keeps track of how many times the light has been turned off, so do not call this in a loop, or else the light will be unable to turn on.

Room Functions and Variables

A room has a set of textures, a function, and cells are assigned to it.

Variables:

  • category - The category this room is (Class, Faculty, Office, Special, etc) as a string. Cannot be set.
  • mapColor - The color of this room on the map. Changing this will update the map accordingly.
  • powered - The rooms power state. If the room is powered off, then the activity will be as well. Can be changed.
  • name - The name of the room, can be set in the editor. Can be changed.
  • hasActivity - True if this room has an activity/notebook. Cannot be set.
  • activityCompleted - True if this room has an activity/notebook and the activity is currently complete. Cannot be set.

Functions:

  • GetCells()
  • GetEntitySafeCells()
  • GetLights()
  • GetRandomEntitySafeCell()
  • IsHall()
  • GetZone()
  • LockAllDoors()
  • UnlockAllDoors()
  • LockAllDoorsTimed(time)
  • RespawnItem(itemId)
  • RespawnActivity()

GetCells()

Returns all cells belonging to this room.

GetZone()

Returns the zone this room was assigned in the editor, with 0 being no zone.

GetEntitySafeCells()

Returns all entity safe cells belonging to this room.

GetLights()

Get all lights belonging to this room.

GetRandomEntitySafeCell()

Returns a random entity safe cell that belongs to this room, defaulting to the first cell if not found.

IsHall()

Returns if this room is a hall.

LockAllDoors()

Locks all doors connected to this room.

UnlockAllDoors()

Unlocks all doors connected to this room.

LockAllDoorsTimed(time)

Locks all doors connected to this room for the specified amount of time in seconds.

RespawnItem(itemId)

Selects a random item respawn point (a place where an item was or a Potential Item Spawn placed in the editor) and spawns the item of itemId there. If the room had a sticker pack in it, it is advised to avoid using this method.

RespawnActivity()

Instantly causes the activity to become available again, increasing the notebook count by one.

ElevatorManager Functions and Variables

The ElevatorManager is what manages almost everything regarding elevators and their current state.

Variables: This object has no variables

Functions:

  • SetIntendedElevatorState(elevator, state)
  • GetIntendedElevatorState(elevator)
  • SetTotalOutOfOrderElevators(total)
  • SetAllElevators(state)
  • GetElevators()

SetIntendedElevatorState(elevator, state)

Sets the intended state of the Elevator. Refer to the id cheat sheet for the state.

GetIntendedElevatorState(elevator)

Gets the intended state of the Elevator.

SetTotalOutOfOrderElevators(total)

Sets the amount of out of order elevators required. escapeSequenceActive must be true for this to work correctly.

SetAllElevators(state)

Sets the state of all elevators to the passed in state. Refer to the id cheat sheet for the state.

GetElevators()

Returns all elevators this ElevatorManager controls.

Elevator Functions and Variables

The elevator is an elevator/exit. It's state can be changed and gotten.

Variables:

  • cell
  • state
  • powered
  • gateIsOpen

cell

The cell this elevator is on. Cannot be set.

state

The state this elevator is currently at. May not match ElevatorManager.GetIntendedElevatorState, refer to the id cheat sheet. Setting calls SetState

powered

Whether or not this elevator is powered. Cannot be set.

gateIsOpen

Whether or not the gate for this elevator is currently open. Cannot be set.

Functions:

  • SetState(state)

SetState(state)

Sets the state of this elevator. Refer to the id cheat sheet.

IntVector2 Functions and Variables

An IntVector2 is used to represent a cell position. They can be added and subtracted from other IntVector2s.

Variables:

  • x - The x position. Cannot be set.
  • z - The z position. Cannot be set.

Vector3 Functions and Variables

A Vector3 is usually used to represent a position. They can be added and subtracted from other Vector3s.

Variables:

  • x - The x position. Cannot be set.
  • y - The y position. Cannot be set.
  • z - The z position. Cannot be set.

Functions:

  • DistanceFrom(other)

DistanceFrom(other)

This returns the distance from the Vector3 to "other".

Color Functions and Variables

A Color represents a well, color.

Variables:

  • r - The r channel. Cannot be set.
  • g - The g channel. Cannot be set.
  • b - The b channel. Cannot be set.

Functions:

There are no functions for this type.

Clone this wiki locally