Lua in Roblox

Coding in Roblox with Lua

1/ Create scripts

Create script by pressing the (+) sign next to Workspace or a Part.

Rename the script if you like (as you can run multiple scripts per object).

Write comments in script with —

2/ Objects

Dot notation

script.parent to refer to the parent of the script

For every object we have properties, built-in functions, and events.

3/ Change properties

game.Workspace.Part.BrickColor = BrickColor.new(“Really red”)

game.Workspace.Part.Transparency = 0 – – disappear with 0

game.Workspace.Part.Reflectance = 0.5

game.Workspace.Part.Material = “Brick”

game.Workspace.Part.Anchored = true

game.Workspace.Part.CanCollide = false

4/ Referencing a part

part = game.Workspace.myPart

and then:

part.Transparency = 1

5/ Variables

myVariable = “hello”

x = 5

done = true

print(myVariable)

6/ Create new part

part = Instance.new(“Part”) –from advanced objects see what parts are available

part.Parent = game.Workspace

Alternatively:

part = Instance.new(“Part”, game.Workspace)

7/ Positions

part.Position = Vector3.new(0, 0, 0)

8/ Loops & conditionals

— while loop

counter = 0
while counter < 10 do
    local part = Instance.new(“Part”, game.Workspace)
    wait(1)
    counter ++
end 

— for loop

for i = 1, 10, 1 do
    print (i)
end

general form:

for i = x, y, z do

x initial value

y final value

z step

9/ Functions

function myFunction()
 game.Workspace.Baseplate.BrickColor = BrickColor.new(“Really Red”)
end
wait(2)
myFunction()

Parameters

function addition (number1, number2)
    print(number1 + number2)
end
addition(5, 7)

10/ Events

An event listens for something to happen and when it does, it triggers an action. 

Set up an event (listener to events) – Connect it to a function.

View >> Object Browser. See events (lighting icons) per object. You can use any event available for the specific object.

For objects Part there is an event called “Touched”.

Create a Part. Call it “TouchedEventPart”.  

Create a script in Workspace. Inside the script we refer to the TouchedEventPart:

game.Workspace.TouchedEventPart.Touched:Connect(function()
game.Workspace.Baseplate.BrickColor = BrickColor.new("Black")
end)

Another example: Create a Part. Name it PartMurder. When touched it will print “Kill him!”

function killPlayer()
    print ("Kill him!")
end
game.Workspace.PartMurder.Touched:Connect(killPlayer)

Parameters: You can pass to the function a parameter. The type of the parameter is defined if you choose the event and see the input type. Eg for the touched event the parameter is the object which touched the part. 

game.Workspace.TouchedEventPart.Touched:Connect(function(hit)
print (hit.Name)
end)

The instance hit will receive the other part (body part i.e. RightFoot, LeftLeg etc).

game.Workspace.TouchedEventPart.Touched:Connect(function(hit)
    print (hit.Parent.Name)
end)

Print the name of the player.

game.Workspace.TouchedEventPart.Touched:Connect(function(hit)
hit.Parent.Humanoid.Sit = True
end)

Make the player sit. Humanoid is a property for players.

game.Workspace.TouchedEventPart.Touched:Connect(function(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) then
        hit.Parent.Humanoid.Sit = True
    end
end)

11/ If statements

if 2+3 == 5 then
    print (“correct”)
end

 

if game.Lighting.TimeOfDay == “12:00:00” then
    print (“Correct”)
end
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.