> ## Documentation Index
> Fetch the complete documentation index at: https://docs.virusstudio.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Lua Scripting Basics: Variables, Tables & Functions

> Learn the core building blocks of Lua — variables, data types, functions, tables, loops, and conditionals — with hands-on examples for game scripting.

Lua is a lightweight, fast scripting language that powers game logic in Roblox and many other platforms. Whether you are brand new to programming or coming from another language, understanding Lua's fundamentals will help you read, write, and request scripts with confidence. This guide walks you through the essential concepts you will encounter in almost every Lua script.

## Variables and Data Types

In Lua, you declare variables with the `local` keyword. Lua is dynamically typed, so you do not need to specify a type — the value itself determines it.

```lua theme={null}
-- Numbers
local score = 0
local speed = 16.5

-- Strings
local playerName = "Virus"

-- Booleans
local isAlive = true

-- Nil (the absence of a value)
local empty = nil
```

Lua's core data types are **number**, **string**, **boolean**, **nil**, **table**, and **function**. You will use all of them regularly.

## Functions

Functions let you group reusable logic under a name. You can define them with `local function` (scoped) or as values assigned to a variable.

```lua theme={null}
local function greetPlayer(name)
    print("Welcome, " .. name .. "!")
end

greetPlayer("Virus") -- Output: Welcome, Virus!

-- Functions as values
local square = function(n)
    return n * n
end

print(square(5)) -- Output: 25
```

## Tables

Tables are Lua's only data structure — they act as both arrays and dictionaries (key-value maps). They are the backbone of almost every Roblox script.

```lua theme={null}
-- Array-style table
local fruits = { "apple", "banana", "cherry" }
print(fruits[1]) -- Output: apple  (Lua arrays start at 1!)

-- Dictionary-style table
local player = {
    name = "Virus",
    level = 42,
    isAdmin = false,
}
print(player.name)    -- Output: Virus
print(player["level"]) -- Output: 42

-- Adding a new key
player.coins = 500
```

## Loops

Lua provides `for`, `while`, and `repeat...until` loops for iteration.

<CodeGroup>
  ```lua for loop theme={null}
  -- Numeric for loop: counts from 1 to 5
  for i = 1, 5 do
      print("Count: " .. i)
  end

  -- Generic for loop: iterates over a table
  local colors = { "red", "green", "blue" }
  for index, color in ipairs(colors) do
      print(index, color)
  end
  ```

  ```lua while loop theme={null}
  local lives = 3

  while lives > 0 do
      print("Lives remaining: " .. lives)
      lives = lives - 1
  end
  ```

  ```lua repeat loop theme={null}
  local input = 0

  repeat
      input = input + 1
  until input >= 3

  print("Stopped at: " .. input) -- Output: Stopped at: 3
  ```
</CodeGroup>

## Conditionals

Use `if`, `elseif`, and `else` to branch your logic. Lua uses `and`, `or`, and `not` instead of `&&`, `||`, and `!`.

```lua theme={null}
local health = 75

if health <= 0 then
    print("You are dead.")
elseif health < 25 then
    print("Critical health — find cover!")
elseif health < 75 then
    print("You are wounded.")
else
    print("You are at full health.")
end

-- Combining conditions
local hasKey = true
local doorOpen = false

if hasKey and not doorOpen then
    print("Use your key to open the door.")
end
```

## String Concatenation and Length

Lua uses `..` to join strings and `#` to get the length of a string or array-style table.

```lua theme={null}
local first = "Hello"
local second = "World"
print(first .. ", " .. second .. "!") -- Output: Hello, World!

local items = { "sword", "shield", "potion" }
print("You have " .. #items .. " items.") -- Output: You have 3 items.
```

<Tip>
  **Common Lua gotchas to watch out for:**

  * **Arrays start at 1**, not 0. `myTable[1]` is the first element.
  * **`nil` is not the same as `false`.** `nil` means "no value exists"; `false` is a boolean. Both are falsy in conditions, but they are different types.
  * **There is no `null` keyword** — use `nil` instead.
  * **`~=` means "not equal"**, not `!=` like in most other languages.
  * **Global variables are dangerous.** Always use `local` unless you have a specific reason not to — accidental globals cause hard-to-find bugs.
  * **String concatenation with `..` requires spaces around it** to avoid ambiguity with decimal numbers.
</Tip>

***

## Keep Learning

<CardGroup cols={2}>
  <Card title="Roblox Scripting" icon="gamepad" href="/guides/roblox-scripting">
    Put your Lua knowledge to work inside Roblox Studio with scripts, RemoteEvents, and the full game API.
  </Card>

  <Card title="Debugging Tips" icon="bug" href="/guides/debugging-tips">
    Learn how to read Lua error messages, isolate problems, and fix issues faster.
  </Card>
</CardGroup>
