tech/game

21/07/15 Roblox Script Tutorial

tech-lover 2021. 7. 16. 00:15

# Intro to Scripting

https://youtu.be/dWisUXms3Sw

 

- Lua Script - Run a command

 

 

# Variables

https://youtu.be/e1zGdnFZrSg

 

> favoriteNumber = 7

> print(favoriteNumber)

7

 

# Variable Names

https://youtu.be/Jt4XGLMdn_k

player1 : OK

1player : ERROR

and : ERROR (reserved keyword)

 

Basic Math

https://youtu.be/PCbP-v_edpk

> print(7 +5)

12

> var1 = 4

> print(var1 + 2)

6

 

Changing Properties

https://youtu.be/a8VuZNyyV4I

> game.Workspace.Baseplate.Transparency = 0.5

 

Changing Properties Part II

https://youtu.be/hQ08tM4gJWQ

 

Brick Color

https://youtu.be/yG0Z_v8hxbY

 

Adding a Script

https://youtu.be/OpxjDu42wEI

 

Explorer > ServerScriptService

 

```

print("Hello world!")

game.Workspace.Baseplate.Transparency = 0.5

game.Workspace.Baseplate.BrickColor = BrickColor.Blue()

```

Variables in Scripts

https://youtu.be/eoWkegwhYZQ

```

local myBaseplate = game.Workspace.Baseplate

myBaseplate.Transparency = 0.5

myBaseplate.BrickColor = BrickColor.Blue()

```

 

Day/Night Cycle: Time of Day

https://youtu.be/chtrhQ1aBuo

```

game.Lighting:SetMinutesAfterMidnight(4 * 60) -- 4 hour

```

 

Day/Night Cycle: Wait

https://youtu.be/qS_RoGS4AjA

```

game.Lighting:SetMinutesAfterMidnight(4 * 60-- 4 hour

wait(1)

game.Lighting:SetMinutesAfterMidnight(5 * 60-- 5 hour

wait(1)

game.Lighting:SetMinutesAfterMidnight(6 * 60-- 6 hour

wait(1)

game.Lighting:SetMinutesAfterMidnight(7 * 60-- 7 hour

wait(1)

game.Lighting:SetMinutesAfterMidnight(8 * 60-- 8 hour

wait(1)

game.Lighting:SetMinutesAfterMidnight(9 * 60-- 9 hour

wait(1)

game.Lighting:SetMinutesAfterMidnight(10 * 60-- 10 hour

```

Day/Night Cycle: Looping

https://youtu.be/XfniMdHI30c

```

local minutesAfterMidnight = 0

 

while true do

 

  game.Lighting:SetMinutesAfterMidnight( minutesAfterMidnight ) 

  minutesAfterMidnight = minutesAfterMidnight + 1

  wait(0.1)

 

end

```

Day/Night Cycle: Street Light

https://youtu.be/OFGgTHlaWiA

```

local lightPart = script.Parent

local pointLight = lightPart.PointLight

 

while true do

 

  wait(0.1)

  if game.Lighting:GetMinitesAfterMidnight() > 6 * 60 then

    lightPart.Material = Enum.Material.Plastic

    pointLight.Enabled = false

  end

 

  if game.Lighting:GetMinitesAfterMidnight() > 18 * 60 then

    lightPart.Material = Enum.Material.Neon

    pointLight.Enabled = true

  end

 

end

```

 

Print Debugging

https://youtu.be/JpeE1RxNVho

 

```

local lightPart = script.Parent

 

local pointLight = lightPart.PointLight

 

while true do

 

  wait(0.1)

  print(game.Lighting.TimeOfDay)

 

  if game.Lighting:GetMinitesAfterMidnight() > 6 * 60 then

    lightPart.Material = Enum.Material.Plastic

    pointLight.Enabled = false

    print("Light off")

  end

 

  if game.Lighting:GetMinitesAfterMidnight() > 18 * 60 then

    lightPart.Material = Enum.Material.Neon

    pointLight.Enabled = true

    print("Light on")

  end

 

end

```

 

Round Based Game: Functions

https://youtu.be/dWWURNCG2U0

```

local function myFunction()

 

  print("Hello world!")

 

end

 

myFunction()

```

 

Round Based Game: Cloning

https://youtu.be/RoENRFy4ofA

ServerStorage

```

local explosionPart = game.ServerStorage.ExplosionPart

 

local function createExplosionPartCopy()

 

  local explosionPartCopy = explosionPart:Clone()

  explosionPartCopy.Parent = game.Workspace

 

end

 

while true do

 

  createExplosionPartCopy()

  wait(3)

 

end

```

 

Round Based Game: Random Positions

https://youtu.be/rsTN0DppMaA

View > Show Grid

```

local explosionPart = game.ServerStorage.ExplosionPart

local explosionPartDropHeight = 100

 

local function createExplosionPartCopy()

 

    local explosionPartCopy = explosionPart:Clone()

    explosionPartCopy.Parent = game.Workspace

 

    local xPosition = math.random(-6464)

    local zPosition = math.random(-6464)

 

    explosionPartCopy.Position = Vector3.new(xPosition , explosionPartDropHeight , zPosition)

 

end

 

while true do

 

    createExplosionPartCopy()

    wait(0.25)

 

end

```

 

Round Based Game: Game Loop and Intermission

https://youtu.be/oawuZNaM-rI

```

local explosionPart = game.ServerStorage.ExplosionPart

local explosionPartDropHeight = 100

local gameRoundLengthInSeconds = 10

local intermissionLengthInSeconds = 5

local roundStartTime

 

local function initialize()

    roundStartTime = tick()

end

 

local function createExplosionPartCopy()

    local explosionPartCopy = explosionPart:Clone()

    explosionPartCopy.Parent = game.Workspace

 

    local xPosition = math.random(-6464)

    local zPosition = math.random(-6464)

    explosionPartCopy.Position = Vector3.new(xPosition , explosionPartDropHeight , zPosition)

 

end

 

while true do

 

    initialize()

 

    repeat

        local currentTime = tick()

        local timeSinceGameStarted = currentTime - roundStartTime

 

        createExplosionPartCopy()

        wait(0.25)

    until timeSinceGameStarted > gameRoundLengthInSeconds

 

    wait(intermissionLengthInSeconds)

 

end

```

Round Based Game: Map Regenerating

https://youtu.be/AyawHBZXT9g

```

local explosionPart = game.ServerStorage.ExplosionPart

local explosionPartDropHeight = 100

local gameRoundLengthInSeconds = 10

local intermissionLengthInSeconds = 5

local roundStartTime

local map = game.Workspace.map

 

local function initialize()

    local mapCopy = map:Clone()

    mapCopy.Name = "MapCopy"

    mapCopy.Parent = game.Workspace

    roundStartTime = tick()

end

 

local function createExplosionPartCopy()

    local explosionPartCopy = explosionPart:Clone()

    explosionPartCopy.Parent = game.Workspace

 

    local xPosition = math.random(-6464)

    local zPosition = math.random(-6464)

    explosionPartCopy.Position = Vector3.new(xPosition , explosionPartDropHeight , zPosition)

 

end

 

local function cleanup()

    game.Workspace.MapCopy:Destroy()

end

 

map.Parent = game.ServerStorage

while true do

 

    initialize()

 

    repeat

        local currentTime = tick()

        local timeSinceGameStarted = currentTime - roundStartTime

 

        createExplosionPartCopy()

        wait(0.25)

    until timeSinceGameStarted > gameRoundLengthInSeconds

    cleanup()

 

    wait(intermissionLengthInSeconds)

 

end

```

Exploding Part

https://youtu.be/Rj-_cjGfCCY

```

local explosionPart = script.Parent

local explosion = Instance.new("Explosion", game.Workspace)

explosion.Position = explosionPart.Position

explosionPart.Destroy()

```

While Loops

https://youtu.be/Ox_CjgD9_k0

```

local explosionPart = script.Parent



while explosionPart.Velocity.magnitude < 0.1 do

    wait()

end

 

local explosion = Instance.new("Explosion", game.Workspace)

explosion.Position = explosionPart.Position

explosionPart.Destroy()

```

For Loops

https://youtu.be/ysGLJqvTDxQ

```

local explosionPart = script.Parent



while explosionPart.Velocity.magnitude < 0.1 do

    wait()

end

 

local steps = 1

for steps = 13 do

    explosionPart.BrickColor = BrickColor.Red()

    wait(0.5)

    explosionPart.BrickColor = BrickColor.White()

    wait(0.5)

end

 

local explosion = Instance.new("Explosion", game.Workspace)

explosion.Position = explosionPart.Position

explosionPart.Destroy()

```

Scripting a Traffic Light

https://youtu.be/3g3N9tfux8w

```

local trafficLight = script.Parent

local green = trafficLight.Green

local yellow = trafficLight.Yellow

local red = trafficLight.Red

 

while true do

    -- Turn on the green light

    wait(7)

    -- Turn on the yellow light

    wait(3)

    -- Turn on the red light

    wait(10)

end

```

Turning On Lights

https://youtu.be/H-mKrKEGIFU

 

```

local trafficLight = script.Parent

local green = trafficLight.Green

local yellow = trafficLight.Yellow

local red = trafficLight.Red

 

local function turnOn(part)

    part.Material = Enum.Material.Neon

    part.SurfaceLight.Enabled = true

end

 

while true do

    -- Turn on the green light

    turnOn(green)

    wait(7)

    -- Turn on the yellow light

    turnOn(yellow)

    wait(3)

    -- Turn on the red light

    turnOn(red)

    wait(10)

end

```

Turning Off Lights

https://youtu.be/gDABDDqtRHE

```

local trafficLight = script.Parent

local green = trafficLight.Green

local yellow = trafficLight.Yellow

local red = trafficLight.Red

 

local function turnOn(part)

    part.Material = Enum.Material.Neon

    part.SurfaceLight.Enabled = true

end

 

local function turnOff(part)

    part.Material = Enum.Material.Plastic

    part.SurfaceLight.Enabled = false

end

 

while true do

    -- Turn on the green light

    turnOn(green)

    turnOff(red)

    wait(7)

    -- Turn on the yellow light

    turnOn(yellow)

    turnOff(green)

    wait(3)

    -- Turn on the red light

    turnOn(red)

    turnOff(yellow)

    wait(10)

end

```

Multiple Traffic Lights

https://youtu.be/9tPHGVSqSWA

```

local trafficLight = script.Parent

local green = trafficLight.Green

local yellow = trafficLight.Yellow

local red = trafficLight.Red

 

local function turnOn(part)

    part.Material = Enum.Material.Neon

    part.SurfaceLight.Enabled = true

end

 

local function turnOff(part)

    part.Material = Enum.Material.Plastic

    part.SurfaceLight.Enabled = false

end

 

while true do

    -- Turn on the red light

    turnOn(red)

    turnOff(yellow)

    wait(10)

    -- Turn on the green light

    turnOn(green)

    turnOff(red)

    wait(7)

    -- Turn on the yellow light

    turnOn(yellow)

    turnOff(green)

    wait(3)

end

```

Comments

https://youtu.be/fWatg9q0cwU

Lava and Water: Lighting Fires

https://youtu.be/9sBbaF4z3lM

```

-- Lava and Water: Lighting Fires

 

local firePart = script.Parent

 

local function lightOnFire(part)

    local fire = Instance.new("Fire")

    fire.Parent = part

end

 

firePart.Touched:connect(lightOnFire)

```

Lava and Water: Putting Out Fires

https://youtu.be/jyzjwDlgGxI

```

-- Lava and Water: Putting Out Fires

 

local waterPart = script.Parent

 

local function putOutFire(part)

    local fire = part:FindFirstChild("Fire")

    if fire then

        fire:Destroy()

    end

end

 

waterPart.Touched:connect(putOutFire)

```

Lava and Water: Tidying Up

https://youtu.be/tdhcQAsweHs

```

-- Lava and Water: Tidying Up

 

local firePart = script.Parent

 

firePart.Trancparency = 1

 

local function lightOnFire(part)

 

    local fire = part:FindFirstChild("Fire")

    if not fire then

        local fire = Instance.new("Fire")

        fire.Parent = part

    end

end

 

firePart.Touched:connect(lightOnFire)

 

```