SCRIPT BOX · TEACHING TEAM

เปิดกล่อง
สคริปต์

รวม Lua Script และ LocalScript จากบทเรียนของวิทยากร เลือกหัวข้อ กดคัดลอก แล้วนำไปทดลองใน Roblox Studio พร้อมคำแนะนำตำแหน่งวางโค้ด

COPY · PASTE · PLAY

คลังสคริปต์จากบทเรียน

รวบรวมโค้ดที่ได้รับจากวิทยากรคนที่ 4, 5 และ 6 โดยจัดหมวดและอธิบายสิ่งที่ต้องเตรียมก่อนนำไปใช้

วิทยากรคนที่ 4 · นายธนาดล ใจเมือง

ระบบตัวละคร ลาวา Checkpoint และความเสียหายจากการตก

5 SCRIPTS
04.1 · CHARACTER ANIMATIONSETUP

โหลด Animation เมื่อตัวละครเกิดใหม่

เตรียม Humanoid, Animator และ AnimationTrack ใหม่ทุกครั้งที่ Character ถูกสร้าง

LocalScript — StarterPlayerScripts
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local RUN_ANIMATION_ID = 0 -- ใส่ Animation ID ของคุณ
local NORMAL_SPEED = 16
local runTrack

local function setupCharacter(character)
    local humanoid = character:WaitForChild("Humanoid")
    local animator = humanoid:FindFirstChildOfClass("Animator")
        or Instance.new("Animator", humanoid)

    local animation = Instance.new("Animation")
    animation.AnimationId = "rbxassetid://" .. RUN_ANIMATION_ID

    runTrack = animator:LoadAnimation(animation)
    humanoid.WalkSpeed = NORMAL_SPEED
end

player.CharacterAdded:Connect(setupCharacter)

if player.Character then
    setupCharacter(player.Character)
end

!เปลี่ยน RUN_ANIMATION_ID เป็นเลข Animation ที่เจ้าของ Experience มีสิทธิ์ใช้งาน โค้ดส่วนอื่นจึงค่อยสั่ง runTrack:Play()

04.2 · LAVA FOLDEREVENT

ลาวาหลายก้อนแบบตายทันที

เชื่อม Touched ให้ Part ทุกก้อนในโฟลเดอร์ Lava Bricks

Script — ServerScriptService
local lavaBricks = workspace
    :WaitForChild("Lava Bricks")
    :GetChildren()

for _, lavaBrick in ipairs(lavaBricks) do
    lavaBrick.Touched:Connect(function(hit)
        local character = hit:FindFirstAncestorOfClass("Model")
        local humanoid = character
            and character:FindFirstChildOfClass("Humanoid")

        if humanoid then
            humanoid.Health = 0
        end
    end)
end

!สร้าง Folder ใน Workspace ชื่อ Lava Bricks แล้ววาง Part ลาวาทุกก้อนไว้ภายใน

04.3 · DAMAGE LAVAEVENT

ลาวาลดพลังชีวิต

สร้างลาวาที่ลด Health ครั้งละ 10 และมีคูลดาวน์แยกตามตัวละคร

Script — Lava Part
local lava = script.Parent
local DAMAGE = 10
local COOLDOWN = 1
local coolingDown = {}

lava.Touched:Connect(function(hit)
    local character = hit:FindFirstAncestorOfClass("Model")
    local humanoid = character
        and character:FindFirstChildOfClass("Humanoid")

    if not humanoid or coolingDown[humanoid] then
        return
    end

    coolingDown[humanoid] = true
    humanoid:TakeDamage(DAMAGE)

    task.delay(COOLDOWN, function()
        coolingDown[humanoid] = nil
    end)
end)

!วาง Script ไว้ใน Part ลาวา ปรับ DAMAGE และ COOLDOWN เพื่อเปลี่ยนความยาก

04.4 · CHECKPOINT COLORSYSTEM

จับคู่ Checkpoint กับ Team

ตั้ง TeamColor ของ Checkpoint ตาม Team ที่มีชื่อเดียวกัน

Script — ServerScriptService
local Teams = game:GetService("Teams")
local checkpoints = workspace
    :WaitForChild("Checkpoints")
    :GetChildren()

for _, checkpoint in ipairs(checkpoints) do
    local team = Teams:FindFirstChild(checkpoint.Name)

    if team and checkpoint:IsA("SpawnLocation") then
        checkpoint.TeamColor = team.TeamColor
    end
end

!ชื่อ SpawnLocation ในโฟลเดอร์ Checkpoints ต้องตรงกับชื่อ Team ทุกตัวอักษร

04.5 · FALL DAMAGESYSTEM

ความเสียหายจากการตก

คำนวณ Damage ตามความสูงที่ตก โดยกำหนดช่วงปลอดภัยและช่วงเสียชีวิต

Script — ServerScriptService
local Players = game:GetService("Players")
local MIN_HEIGHT = 5
local MAX_HEIGHT = 25

local function calculateFallDamage(fallHeight, maxHealth)
    if fallHeight <= MIN_HEIGHT then
        return 0
    elseif fallHeight >= MAX_HEIGHT then
        return maxHealth
    end

    local ratio = (fallHeight - MIN_HEIGHT)
        / (MAX_HEIGHT - MIN_HEIGHT)
    return ratio * maxHealth
end

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        local rootPart = character:WaitForChild("HumanoidRootPart")
        local initialHeight

        humanoid.FreeFalling:Connect(function(isFalling)
            if isFalling then
                initialHeight = rootPart.Position.Y
            elseif initialHeight then
                local fallHeight = initialHeight - rootPart.Position.Y
                local damage = calculateFallDamage(
                    fallHeight,
                    humanoid.MaxHealth
                )

                humanoid:TakeDamage(damage)
                initialHeight = nil
            end
        end)
    end)
end)

!วางใน ServerScriptService แล้วปรับ MIN_HEIGHT และ MAX_HEIGHT ให้เหมาะกับสเกลของแมพ

วิทยากรคนที่ 5 · นายมงคล จาดสกุล (มองข่อย)

พื้นกับดัก การเคลื่อนที่ และเสียงประกอบภายในเกม

6 SCRIPTS
01 · TOUCHED EVENTBEGINNER

พื้นกับดัก

เหยียบแล้วกะพริบเตือน 3 รอบ จากนั้นพื้นยุบและร่วงลง

Script — Trap
local trap = script.Parent
local activated = false

trap.Touched:Connect(function(hit)
    local character = hit.Parent
    local humanoid = character:FindFirstChildOfClass("Humanoid")

    if not humanoid or activated then
        return
    end

    activated = true

    for i = 1, 3 do
        trap.Transparency = 0.5
        task.wait(0.4)
        trap.Transparency = 0
        task.wait(0.4)
    end

    trap.CanCollide = false
    trap.Anchored = false
end)

!วาง Script ภายใน Part ชื่อ Trap และตั้ง Anchored กับ CanCollide เป็น true

01.2 · RESPAWN TRAPOPTION

พื้นกับดักแบบหาย 3 วินาทีแล้วกลับมา

เหยียบแล้วกะพริบเตือน 3 รอบ จากนั้นพื้นหายไป 3 วินาที ก่อนกลับมาและพร้อมทำงานรอบใหม่

Script — Respawn Trap
local trap = script.Parent
local activated = false

trap.Touched:Connect(function(hit)
    local character = hit.Parent
    local humanoid = character:FindFirstChildOfClass("Humanoid")

    if not humanoid or activated then
        return
    end

    activated = true

    for i = 1, 3 do
        trap.Transparency = 0.5
        task.wait(0.4)
        trap.Transparency = 0
        task.wait(0.4)
    end

    trap.Transparency = 1
    trap.CanCollide = false
    task.wait(3)
    trap.Transparency = 0
    trap.CanCollide = true
    activated = false
end)

!วาง Script ภายใน Part ชื่อ Trap และคง Anchored กับ CanCollide เป็น true เพื่อให้พื้นกลับมาอยู่ตำแหน่งเดิม

02 · LOOP & CFRAMEBEGINNER

ใบพัดหมุน

หมุน Spinner รอบแกน Y ต่อเนื่องด้วยลูปและเวลาของแต่ละเฟรม

Script — Spinner
local part = script.Parent
local rotationSpeed = 90

while true do
    local dt = task.wait()
    local rotationAmount = math.rad(rotationSpeed * dt)

    part.CFrame = part.CFrame *
        CFrame.Angles(0, rotationAmount, 0)
end

!วาง Script ภายใน Spinner ตั้ง Anchored เป็น true และลองเปลี่ยน rotationSpeed จาก 90 เป็น 180

03 · VECTOR3 & LERPLEVEL UP

แพลตฟอร์มเคลื่อนที่

เคลื่อนจาก pointA ไป pointB แล้วกลับ พร้อมพักปลายทาง 1 วินาที

Script — MovingPlatform
local platform = script.Parent

local pointA = platform.Position
local pointB = pointA + Vector3.new(0, 0, 30)

while true do
    for i = 0, 1, 0.02 do
        platform.Position = pointA:Lerp(pointB, i)
        task.wait()
    end

    task.wait(1)

    for i = 0, 1, 0.02 do
        platform.Position = pointB:Lerp(pointA, i)
        task.wait()
    end

    task.wait(1)
end

!วาง Script ภายใน MovingPlatform ตั้ง Anchored และ CanCollide เป็น true; เปลี่ยนเป็น Vector3.new(0, 25, 0) เพื่อทำลิฟต์

04 · TRAP & SOUNDSOUND

พื้นกับดักพร้อมเสียง

Script กับดักฉบับเต็ม เพิ่มเสียงหลังล็อกสถานะและก่อนเริ่มกะพริบ

Script — Trap + Sound
local trap = script.Parent
local activated = false

trap.Touched:Connect(function(hit)
    local character = hit.Parent
    local humanoid = character:FindFirstChildOfClass("Humanoid")

    if not humanoid or activated then
        return
    end

    activated = true

    local sound = trap:WaitForChild("Sound")
    sound:Play()

    for i = 1, 3 do
        trap.Transparency = 0.5
        task.wait(0.4)
        trap.Transparency = 0
        task.wait(0.4)
    end

    trap.CanCollide = false
    trap.Anchored = false
end)

!เพิ่มวัตถุ Sound ภายใน Trap แล้วตั้งชื่อวัตถุว่า Sound ให้ตรงกับโค้ด

05 · AMBIENT MUSICSOUND

เสียงบรรยากาศทั่วแมพ

เล่น AmbientMusic วนซ้ำทั่ว Workspace ด้วยระดับเสียง 0.3

Script — Workspace
local music = workspace:WaitForChild("AmbientMusic")
music.Looped = true
music.Volume = 0.3
music:Play()

!วาง AmbientMusic และ Script ไว้ภายใน Workspace ระดับเดียวกัน; ตั้ง Looped ก่อนสั่ง Play()

วิทยากรคนที่ 6 · นายกนกพงศ์ หัวคำ (พี่บาส)

Welcome Board และปุ่มควบคุมเสียงด้วย Roblox GUI

2 SCRIPTS
06.1 · WELCOME BOARDGUI

ปุ่มปิดหน้าต่างต้อนรับ

กดปุ่มแล้วซ่อน Welcome Frame ทั้งชุดด้วย LocalScript

LocalScript — Welcome Frame
local button = script.Parent:WaitForChild("button")

button.MouseButton1Click:Connect(function()
    local frame = script.Parent
    frame.Visible = false
end)

!วาง LocalScript ใน Frame และสร้างปุ่มชื่อ button เป็นลูกของ Frame

06.2 · SOUND CONTROLLERGUI + SOUND

ปุ่มสลับปิด–เปิดเสียง

ใช้ TextButton ควบคุม Volume ของเพลงพื้นหลังและเปลี่ยนข้อความบนปุ่ม

LocalScript — SoundButton
local button = script.Parent
local soundService = game:GetService("SoundService")

local sound = soundService:WaitForChild("Sound")
local isMuted = false

button.MouseButton1Click:Connect(function()
    isMuted = not isMuted

    if isMuted then
        sound.Volume = 0
        button.Text = "🔊 เปิดเสียง"
    else
        sound.Volume = 0.5
        button.Text = "🔇 ปิดเสียง"
    end
end)

!เตรียม SoundService > Sound และวาง LocalScript นี้ไว้ภายใน TextButton ชื่อ SoundButton

ภารกิจของวันนี้: เปลี่ยนเพียง 1 ค่า

เริ่มจากสคริปต์ที่ทำงานได้ก่อน แล้วปรับ Damage ระยะทาง ความเร็ว หรือ Volume ทีละจุด จากนั้นทดสอบผลลัพธ์ใน Roblox Studio

กลับไปเลือกสไลด์ของวิทยากร ↗
ค้นหา ↑