1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🚗 Partをクリックしたら車が出現!【Roblox開発Tips】

1
Posted at

image.png

Roblox StudioでPartをクリックしたら、車のモデルが出現するスクリプトを紹介します。初心者の方でも、モデルの配置やイベント処理の基礎を体感できる内容です!

名称未設定のデザイン (1).gif


🧱 1. 事前準備

🗂 ReplicatedStorageに車モデルを入れる

  1. Explorer(エクスプローラー)ウィンドウを開きます。
  2. ReplicatedStorageを右クリック → Insert ObjectModel(またはPrefabとして読み込んだ車オブジェクト)を追加。
  3. そのモデルの名前を "Car" に変更してください(大文字小文字に注意!)。

image.png


🧱 2. ClickDetectorを仕込むPartを配置

  1. WorkspacePart を1つ設置します(例:LookPartなど)。
  2. このPartをクリックすることで、車が出現します。

💡 3. スクリプトの実装

下記のスクリプトをその Part の中に Script として挿入してください(ScriptServerScript を推奨)。

image.png

local part = script.Parent
local replicatedStorage = game:GetService("ReplicatedStorage")
local carTemplate = replicatedStorage:FindFirstChild("Car") -- ここに車モデルを入れてください

local function spawnCar(player)
    if carTemplate then
        local car = carTemplate:Clone()
        car.Parent = workspace
        -- パートの横に車を出す
        if car:IsA("Model") then
            local partCFrame = part.CFrame
            local carCFrame = partCFrame + Vector3.new(5, 0, 0)
            car:PivotTo(carCFrame)
        elseif car:IsA("BasePart") then
            car.CFrame = part.CFrame + Vector3.new(5, 0, 0)
        end
    else
        warn("ReplicatedStorageに'Car'モデルがありません。")
    end
end

local clickDetector = part:FindFirstChildOfClass("ClickDetector")
if not clickDetector then
    clickDetector = Instance.new("ClickDetector")
    clickDetector.Parent = part
end

clickDetector.MouseClick:Connect(spawnCar)

🔎 解説

  • ReplicatedStorage から Car モデルを取得し、Clone() して Workspace に出現。
  • Part の右に車を並べるように、Vector3.new(5, 0, 0) で位置をずらしています。
  • Model の場合は PivotToBasePart の場合は CFrame で位置調整。
  • ClickDetector がない場合は自動的に追加します。

✅ 動作確認

プレイモードで Part をクリックすると、横に車が出現すれば成功です!

名称未設定のデザイン (1).gif


📝 おまけTips

  • 出現位置を math.random() でランダム化したり、
  • 複数種類の車を選んで出せるように拡張したり、
  • クールダウンをつけて連打防止することもできます。

📦 YouTubeで見たい方はこちら


🙌 おわりに

このような「クリック→出現」のしくみは、ゲーム内でアイテムを生成したり、ギミックを発動したりする時にも応用できます。ぜひ自分のゲームに取り入れてみてください!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?