# Intro to Scripting
- Lua Script - Run a command
# Variables
> favoriteNumber = 7
> print(favoriteNumber)
7
# Variable Names
player1 : OK
1player : ERROR
and : ERROR (reserved keyword)
Basic Math
> print(7 +5)
12
> var1 = 4
> print(var1 + 2)
6
Changing Properties
> game.Workspace.Baseplate.Transparency = 0.5
Changing Properties Part II
Brick Color
Adding a Script
Explorer > ServerScriptService
```
print("Hello world!")
game.Workspace.Baseplate.Transparency = 0.5
game.Workspace.Baseplate.BrickColor = BrickColor.Blue()
```
Variables in Scripts
```
local myBaseplate = game.Workspace.Baseplate
myBaseplate.Transparency = 0.5
myBaseplate.BrickColor = BrickColor.Blue()
```
Day/Night Cycle: Time of Day
```
game.Lighting:SetMinutesAfterMidnight(4 * 60) -- 4 hour
```
Day/Night Cycle: Wait
```
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
```
local minutesAfterMidnight = 0
while true do
game.Lighting:SetMinutesAfterMidnight( minutesAfterMidnight )
minutesAfterMidnight = minutesAfterMidnight + 1
wait(0.1)
end
```
Day/Night Cycle: Street Light
```
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
```
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
```
local function myFunction()
print("Hello world!")
end
myFunction()
```
Round Based Game: Cloning
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
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(-64, 64)
local zPosition = math.random(-64, 64)
explosionPartCopy.Position = Vector3.new(xPosition , explosionPartDropHeight , zPosition)
end
while true do
createExplosionPartCopy()
wait(0.25)
end
```
Round Based Game: Game Loop and Intermission
```
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(-64, 64)
local zPosition = math.random(-64, 64)
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
```
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(-64, 64)
local zPosition = math.random(-64, 64)
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
```
local explosionPart = script.Parent
local explosion = Instance.new("Explosion", game.Workspace)
explosion.Position = explosionPart.Position
explosionPart.Destroy()
```
While Loops
```
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
```
local explosionPart = script.Parent
while explosionPart.Velocity.magnitude < 0.1 do
wait()
end
local steps = 1
for steps = 1, 3 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
```
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
```
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
```
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
```
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
Lava and Water: Lighting Fires
```
-- 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
```
-- 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
```
-- 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)
```
'tech > game' 카테고리의 다른 글
21/07/16 ROBLOX University - Mad Bloxxer (0) | 2021.07.16 |
---|---|
21/07/16 ROBLOX University ROBLOX Racing (0) | 2021.07.16 |
21/07/15 How to change your system language in roblox studio (0) | 2021.07.15 |
21/07/15 Godot Tutorial #2 (0) | 2021.07.15 |
21/07/15 Godot Tutorial #1 (0) | 2021.07.15 |