サイトアイコン schilverberch★ROBLOX

第20回 ~鍵を拾ってドアを開ける~

ROBLOX★プログラミング講座【入門編】第20回 ~鍵を拾ってドアを開ける~

ツール(Tool)の基本的な仕組みを理解します。ツールボックスにある鍵を使ってドアを開けるというプログラムを作ります。

登場する英単語

Starter Packスターターパック初めから所持するツールはここに入れる
Playersプレイヤーズgame下に位置するオブジェクト、ゲームにいるプレイヤーが入る
PlayerプレイヤーPlayers下に位置するオブジェクト、プレイヤー情報
CharacterキャラクターPlayerのプロパティ、Workspaceに表示されているキャラクターモデルが入る
Backpackバックパックプレイヤーが所持しているツールが入る場所
Handleハンドルツールに存在するパーツ

チェックポイント

local door = script.Parent
local touch = false

door.Touched:Connect(function(hit)
    local character = hit.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")

    if humanoid and touch == false then
        local key = character:FindFirstChild("Key")
        if key then
            touch = true
            door.CanCollide = false
            door.Transparency = 1
            wait(1)
            key:Destroy()
            door.CanCollide = true
            door.Transparency = 0
            touch = false
        end
    end
end)
local keyBox = script.Parent
local clickdetector = keyBox.ClickDetector

local function checkHaveItem(player,item)
    local character = player.Character

    if character:FindFirstChild(item) then
        return true
    end
    if player.Backpack:FindFirstChild(item) then
        return true
    end 
    return false
end

clickdetector.MouseClick:Connect(function(player)
    if checkHaveItem(player,"Key") == false then
        local key = keyBox.Key
        local item = key:Clone()
        item.Parent = player.Backpack
    end
end)
モバイルバージョンを終了