Robloxでシンプルなコイン拾いゲームを作成する
プロジェクトの作成
新規プロジェクトをBaseTemplateで作成する

コインパーツの作成
シリンダーパーツを作成し、Coinに名前を変更する

Coinのスケールを(1, 4, 4)に変更する

CoinのマテリアルにMetal、色にNewYellerを選択する




コインを獲得できるようにする
Coinパーツ配下にスクリプトを追加、名前をCoinScriptにする

CoinScriptを以下のように編集する
-- coinのパーツを取得 local part = script.Parent -- 触れられたパーツがプレイヤーだったらcoinを破壊する local function onPartTouch(otherPart) local partParent = otherPart.Parent local humanoid = partParent:FindFirstChildWhichIsA("Humanoid") if humanoid then part:Destroy() end end -- coinパーツがなにかに触れられたときにonPartTouchを実行する part.Touched:Connect(onPartTouch)

コインをランダムな位置に生成する
現状ではコインを一枚取ってしまったらゲームが終わってしまうため、ランダムな位置にコインを生成できるようにする
作成したCoinをServerStorage配下に移動する

ServerScriptServiceに新しいスクリプトを作成し、CoinSpawnScriptに名前を変更する

以下のように記述する
-- ServerStorageにアクセスするためのサービスクラスを取得 local storage = game:GetService("ServerStorage") -- ランダムな位置にコインを生成する local function SpawnCoin() -- ServerStorageからコインパーツを取得、クローンを作成する local coin = storage:FindFirstChild("Coin"):Clone() -- 作成したクローンをランダムな位置に配置する local x = math.random(-50, 50) local z = math.random(-50, 50) coin.Parent = game.Workspace coin.Position = Vector3.new(x, 5, z) end -- 1秒ごとにコイン生成を行う while(true) do SpawnCoin() wait(1) end

このままではCoinが永遠に生成されてしまうため、フィールド上にCoinが10枚以上ある場合、それ以上生成しないようにする
CoinSpawnScriptのSpawnCoinを以下のように改修する
-- ランダムな位置にコインを生成する local function SpawnCoin() -- Workspace内のコインの数を数える local children = workspace:GetChildren() local coinCount = 0 for _, child in next, children do if child.name == "Coin" then coinCount += 1 end end -- コインが10枚以上あれば生成は行わない if coinCount >= 10 then return end -- ServerStorageからコインパーツを取得、クローンを作成する local coin = storage:FindFirstChild("Coin"):Clone() -- 作成したクローンをランダムな位置に配置する local x = math.random(-50, 50) local z = math.random(-50, 50) coin.Parent = game.Workspace coin.Position = Vector3.new(x, 5, z) end

スコアを表示する
拾ったコインの数の数をリーダーボードを利用して表示する
ServerScriptService配下にCoinAmountLeaderBoardに名前を変更する

CoinAmountLeaderBoardにてリーダーボードを初期化する
local Players = game:GetService("Players") -- リーダーボードを初期化する local function leaderboardSetup(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local stage = Instance.new("IntValue") stage.Name = "Coin" stage.Value = 0 stage.Parent = leaderstats end -- プレイヤーが追加されたときにリーダーボードを初期化する Players.PlayerAdded:Connect(leaderboardSetup)
ゲームをプレイするとリーダーボードが表示される

コインを獲得したときにリーダーボードを更新する
ServerStorageのCoin配下にあるCoinScriptを編集する

追加するのはPlayerのサービスクラスの獲得と、コインを獲得したときに獲得したプレイヤーのリーダーボードを更新する処理
-- プレイヤーのサービスクラスを取得 local Players = game:GetService("Players") -- coinのパーツを取得 local part = script.Parent -- 触れられたパーツがプレイヤーだったらcoinを破壊する local function onPartTouch(otherPart) local partParent = otherPart.Parent local humanoid = partParent:FindFirstChildWhichIsA("Humanoid") if humanoid then part:Destroy() -- Playerサービスクラスからパーツを触ったプレイヤーを取得 local player = Players:GetPlayerFromCharacter(partParent) -- プレイヤーからleaderstasを取得し、更新する local leaderstats = player.leaderstats local stage = leaderstats and leaderstats:FindFirstChild("Coin") if stage then stage.Value += 1 end end end -- coinパーツがなにかに触れられたときにonPartTouchを実行する part.Touched:Connect(onPartTouch)
結果
