Roblox Basics

Lua Roblox Datatypes

Roblox Datatypes

Lua Roblox Datatypes include Vector3 and CFrame for 3D.

Introduction to Roblox Datatypes

Roblox uses a custom implementation of Lua scripting language, which includes several datatypes tailored for 3D game development. Understanding these datatypes is crucial for creating immersive and interactive environments in Roblox. Two of the most important datatypes are Vector3 and CFrame.

Vector3: The Basics

Vector3 is a datatype used to represent a point in 3D space, defined by three numerical values: X, Y, and Z. It is commonly used for positioning objects, determining velocities, and setting forces in the game world.

-- Creating a Vector3 instance
local position = Vector3.new(10, 20, 30)
print(position)  -- Output: 10, 20, 30

Common Vector3 Operations

Vector3 allows for various arithmetic operations, which can be useful for manipulating object positions and directions. Here are some common operations:

  • Addition: Combine two Vector3 values.
  • Subtraction: Find the difference between two Vector3 values.
  • Multiplication: Scale a Vector3 by a number.
  • Division: Scale down a Vector3 by a number.
local v1 = Vector3.new(1, 2, 3)
local v2 = Vector3.new(4, 5, 6)

local sum = v1 + v2  -- Output: 5, 7, 9
local difference = v1 - v2  -- Output: -3, -3, -3
local scaled = v1 * 2  -- Output: 2, 4, 6
local downscaled = v2 / 2  -- Output: 2, 2.5, 3

CFrame: An Overview

CFrame stands for Coordinate Frame and is a datatype that represents not just position, but also orientation in 3D space. It is essential for correctly orienting objects and cameras in Roblox games.

-- Creating a CFrame instance
local orientation = CFrame.new(0, 10, 0)
print(orientation)  -- Output: (0, 10, 0)

Using CFrame for Rotations

CFrame is particularly powerful when it comes to rotations. You can rotate objects around any axis using CFrame.Angles.

local rotation = CFrame.Angles(math.rad(90), math.rad(0), math.rad(0))

-- Applying rotation to a part
local part = Instance.new('Part')
part.CFrame = CFrame.new(0, 10, 0) * rotation

Combining Vector3 and CFrame

Vector3 and CFrame often work together. For instance, you might position an object using a Vector3, then orient it using a CFrame.

local position = Vector3.new(10, 0, 0)
local orientation = CFrame.Angles(0, math.rad(45), 0)

-- Combine position and orientation
local combinedCFrame = CFrame.new(position) * orientation

local part = Instance.new('Part')
part.CFrame = combinedCFrame
SQL Syntax T-Shirts
No t-shirts available for this site.