LogoLogo
Join The CommunityCommunity ResourcesLuafaq.orgLua Org
  • Welcome
  • Frequently Asked Questions (FAQ)
  • Legacy API
    • Test The API
    • Warning
    • Character
      • v1/wear
      • v1/remove
    • Games
      • v1/games/details
    • User
      • v1/user/request-data
      • v1/user/avatar
      • v1/user/get-by-username
      • v1/user/get-token
      • v1/user/verify-password
      • v1/user/login
      • v1/user/chat/send-message
      • v1/user/chat/chat-summary
      • v1/user/get-messages
      • v1/user/chat/chats
      • v1/user/friends
      • v1/user/friend
      • v1/user/search
    • Guild
      • v1/join
    • Asset
      • v1/sell
      • v1/take-offsale
      • v1/purchase
      • v1/purchase-reseller
      • v1/create
      • v1/get
  • OPEN CLOUD
    • READ ME
    • NPM Package
      • Introduction
      • Tutorials
        • Webhook Configuration
      • Bot Account
      • OpenCloudClient
      • WebAPICliet
        • Client
        • login
        • quit
        • Friends Endpoint
          • SendFriendRequest
          • GetFriends
        • User Endpoint
          • GetAvatar
          • GetPlayerByUsername
        • Guild Endpoint
          • JoinGuild
    • Open Cloud
    • Webhooks
    • Asset Types
    • Beautiful JSON Display
    • V2 API
      • Asset
        • Edit
      • Publish
        • Asset
        • World
      • Feed
        • reply
        • all/{page_cursor/{feed_cursor}
      • Cloud
        • oauth
          • create
        • git/{channel}
        • version
        • database
          • get
          • set
        • credentials
          • list
          • delete
          • create
        • bricklinks
          • list
          • delete
          • create
      • Webhook
        • delete
        • create
        • list
      • User
        • feed
          • send
        • search
        • register
        • login
        • username-available
        • email-available
        • phone-available
        • validate-beta-key
        • id
        • username
        • edit-setting
        • transparency-request
      • Auth
        • send-beta-key
        • get-active-sessions
        • terminate-session
        • get-token
        • session
        • is-authed
        • join-game
        • bot-login
      • Notifications
        • unread
        • all
        • erase
      • Worlds
        • /{universe}/{server}/server/metadata/update/{token}
        • /{universe}/shutdown/all/{token}
        • /{universe}/shutdown/server/{server}/{token}
        • /{universe}/join/server/{server_id}
        • /{universe}/join/random
        • /{universe}/join/user/{user_id}
        • /client/{join_key}
        • /create-universe/{ownerId}/{ownerType}
        • /create-world/{universeId}
        • /worldtree/{worldId}
  • Developer Guide
    • Empowering Responsible Gameplay: A Guide to ModerationService in BrickVerse
    • Empowering Game Creators: A Deep Dive into ENVService with Lua API in BrickVerse
    • Leveraging ENVService as an FFlag System
    • What's Authority?
    • Roblox Vs BrickVerse Classes
    • DRM - Digital Rights Management
  • BrickLua - Coding With Lua
    • Introduction
    • Learn Lua
      • Functions
      • Strings
      • Tables
      • Boolean
      • Conditional Structures
      • Opeartors
      • bit32
      • Numbers
    • Player Event
  • GAME API
    • API
      • Intro
      • math
      • debug
      • coroutine
      • Enum
        • UserInput
        • Humanoid
    • Classes
      • ServiceProvider
        • Enum
        • InteractionService
        • ENVService
        • Void
        • WorldstoreService
        • WorldSettings
        • DiscordRichPresence
        • ClientGui
        • AuthorityService
        • SharedStorage
        • WebService
        • RunService
        • NetworkService
        • AdService
        • UserInputService
        • Scene
        • ChatService
        • Lighting
        • SoundService
        • Players
        • ServerStorage
        • ScriptService
      • DataType
        • CFrame
        • Instance
        • Vector2
        • Vector3
        • Color
      • Dynamic
        • Dynamic3D
          • SpotLight
          • SpawnPart
          • Projection3D
          • PartMaterial
          • OmniLight
          • BasePart
          • InteractionPrompt
          • Page
          • DirectionalLight
          • Brick
          • BasePartConstraint
        • ScriptDynamic
          • ScriptModule
        • BaseUI
          • ScrollFrame
          • ImageButton
          • TextButton
          • ImageLabel
          • TextLabel
          • Frame
          • ViewportFrame
          • ScreenUI
  • Employment
    • List Of Administrators
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. BrickLua - Coding With Lua
  2. Learn Lua

Tables

PreviousStringsNextBoolean

Last updated 1 year ago

Was this helpful?

A table is a Lua data type that can store multiple values including , , , , and more. Tables are constructed with curly braces ({}) as shown here:

-- Construct an empty table assigned to variable "t"
local t = {}
print(t)

Once constructed, a table can behave as either an array or a dictionary as illustrated in the following sections.

Arrays

An array is a simple list of ordered values, useful for storing collections of data such as a group of players with special permissions.

Creating Arrays

To create an array using a Lua table, simply store the values sequentially, separated by commas. An array value can be any non-nil type (boolean, number, string, function, userdata, or even another table).

-- Construct an array with three items
local testArray = {"A string", 3.14159, workspace.Part}

Reading From Arrays

To read from an array, add a pair of brackets after its reference and specify the index number of the element inside ([pos]):

Unlike some languages, Lua uses 1-based indexing for arrays, meaning that the first item in the array is [1], not [0].

-- Construct an array with three items
local testArray = {"A string", 3.14159, workspace.Part}
 
print(testArray[1])
print(testArray[2])
print(testArray[3])l

Writing Into Arrays

The value of an array index can be defined or rewritten by indicating the index number in brackets ([pos]) followed by = and then the value:

local testArray = {"A string", 3.14159, workspace.Part}
 
testArray[2] = 12345
testArray[4] = "New string"
 
print(testArray[2])
print(testArray[4])

Iterating Over Arrays

Arrays can be iterated over (looped through) in two ways:

  • Use the built-in ipairs() function in a for loop.

  • Get the array’s length using the # operator and loop from 1 to that length value.

local testArray = {"A string", 3.14159, workspace.Part, "New string"}
 
-- Loop using "ipairs()"
for index, value in ipairs(testArray) do
	print(index, value)
end
 
-- Iterate using the array length operator (#)
for index = 1, #testArray do
	print(index, testArray[index])
end

Inserting Items

An item can be inserted at the end of an array through either of these methods:

  • Pass the array reference and the item value to Lua’s table.insert() function.

  • Add the new item to the array using the t[#t+1] syntax.

local testArray = {"A string", 3.14159}
 
table.insert(testArray, "New string")
testArray[#testArray+1] = "Another new string"
 
print(testArray[3])
print(testArray[4])
numbers
booleans
strings
functions