チャプター 5

Step 03

イベントを通してスコアボードにスコアを表示させる!

イベントを使う!

ここでは初めてEvent(イベント)を作ります。ゲームプログラミングでとても大切な考え方の一つがイベントでした。これからRobloxでプログラミングをやるにはこのEventはとても大切なテクニックなので、まずはどんなものか体験してみてください。詳しいことはこれからじっくり勉強していけば大丈夫です!

RemoteEventを作る

PlayerからもTrophyからもスコアの更新ができるよう、その間とを取りもつのがRemoteEvent(リモートイベント)です。
➊ エクスプローラーでReplicatedStorageをクリックし、+を押してRemoteEventを追加する。
❷ 名前をScoreChangedEventに変更する。

Playerの当たり判定でスコアを更新する

ではまず、PlayerがTrophyやFireBallに当たった時にスコアを増やしたり減らしたりするコードを書きます。
➊ StarterPlayerの下にあるPlayerHitスクリプトを開く
❷ ここで追加するのはわずか4行で、★のついたところ探してコピペしてください。変更内容はビデオで詳しく解説しています。
--★スコアを変更するScoreEvent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ScoreChangedEvent = ReplicatedStorage:WaitForChild("ScoreChangedEvent")

-- 自分のPlayerを取得
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

-- サウンドを追加
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://2865228021"
sound.Parent = player

--ビープ音を追加
local beep = Instance.new("Sound")
beep.SoundId = "rbxassetid://1584394759"
beep.Parent = player

-- すでに当たったFireBallを記録するテーブルを作成
local touchedFireBalls = {}
-- すでに当たったTrophyを記録するテーブルを作成
local touchedTrophies = {}

-- 体がどこから当たったら呼び出される関数
local function onTouch(hitObj)
-- 当たったものがFireBallだったら
if hitObj.Name == "FireBall" then
-- まだこのFireBallに当たっていなかったら
if not touchedFireBalls[hitObj] then
ScoreChangedEvent:FireServer(-5) --★点数を5点減点
-- サウンドを再生
sound:Play()
-- このFireBallに当たったことを記録
touchedFireBalls[hitObj] = true
end
--当たったものがGoldenTrophyだったら
elseif hitObj.Name == "GoldenTrophy" then
if not touchedTrophies[hitObj] then
ScoreChangedEvent:FireServer(10) --★点数を10点加点
beep:Play()
touchedTrophies[hitObj] = true
end
end
end

-- 体が当たったら呼び出す関数を決める
humanoid.Touched:Connect(onTouch)




Trophyの当たり判定でスコアを更新する

最後に、TrophyとFireBallが当たった時のスコア更新を加えます。
➊ ServerScriptServiceにあるGenerateTrophyスクリプトを開く
❷ ここで追加するのはわずか3行ですが、一部変更したところがあるのでスクリプト全部をコピペで入れかえてください。★のついたところが変更したところで、内容はビデオで詳しく解説しています。
--★スコアを更新するためのスコアモジュールを取得する
local ScoreModule = require(game.ServerScriptService:WaitForChild("ScoreModule"))

-- ランダムな位置を生成する関数
function getRandomPosition()
local x = math.random(-50, 50)
local y = 10 -- 少し浮かせた位置に配置
local z = math.random(-50, 50)
return Vector3.new(x, y, z)
end

-- トロフィーの生成関数
function createTrophy()
local trophy = Instance.new("Part")
trophy.Size = Vector3.new(2, 10, 2) -- トロフィーのサイズ
trophy.Position = getRandomPosition() -- ランダムな位置に配置
trophy.Anchored = false
trophy.CanCollide = true
trophy.BrickColor = BrickColor.new("Bright yellow") -- 黄金色
trophy.Name = "GoldenTrophy"
trophy.Parent = game:GetService("Workspace")

-- ビープを追加
local beep = Instance.new("Sound")
beep.SoundId = "rbxassetid://9113085665"
beep.Parent = trophy

--一度当たったTrophyを保存しておくテーブル(ペナルティボックス)
local touchedFireBalls = {}

local function onTouch(hitObj)
--火の玉が当たったら
if hitObj.Name == "FireBall" and not touchedFireBalls[hitObj] then
touchedFireBalls[hitObj] = true --ペナルティボックスに入れる
beep:Play()
ScoreModule.updateScore(-30) --★ 30点の減点
wait(0.5)
breakTrophy(trophy)
end
end
--トロフィーに触ったというイベントの設定
trophy.Touched:Connect(onTouch)
end

-- トロフィーを壊す関数
function breakTrophy(trophy)
for i = 1, 10 do
local fragment = Instance.new("Part")
fragment.Size = Vector3.new(1, 1, 1)
fragment.Position = trophy.Position + Vector3.new(math.random(-2, 2), math.random(-2, 2), math.random(-2, 2))
fragment.Velocity = Vector3.new(math.random(-50, 50), math.random(50, 100), math.random(-50, 50))
fragment.BrickColor = trophy.BrickColor
fragment.Anchored = false
fragment.CanCollide = true
fragment.Parent = game:GetService("Workspace")
game:GetService("Debris"):AddItem(fragment, 5) -- 5秒後に破片を削除
end
trophy:Destroy()
end

-- トロフィーを5秒ごとに生成
while true do
createTrophy()
wait(5) --5秒に一回出現する
end


これでPlayerの当たり判定でスコアが変わると、スコアボードの点数も変わるようになります!
次にすすむ