【Roblox】プレイヤーがTeamを選び、Team毎にアイテムを持たせる方法

鬼ごっこなどで、鬼役と逃げ役に分かれるゲームを作るときなどに必要な、任意のチーム選びとアイテム付与の方法を教えます。
目次
エクスプローラの設定
RobloxStudioのエクスプローラを以下の構成になるように追加します。

プレイヤーがTeamを選べるように設定する方法
TeamsのチームカラーとSpawnLocationのチームカラーを合わせることで、SpawnLocationにタッチするとそのチームに変更されます。


script:チーム切り替え毎にアイテムを与える / 取り除くプログラム
SpawnLocation(Red Team)の子要素のScriptに以下のプログラムを記述します。
local Teams = game:GetService("Teams")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local redTeam = Teams:FindFirstChild("Red Team")
local toolToGive = ServerStorage:FindFirstChild("ClassicSword")
script.Parent.Touched:Connect(function(hit)
local character = hit.Parent
local player = Players:GetPlayerFromCharacter(character)
if not player then return end
player.Team = redTeam
-- 武器削除(全Toolを削除)
for _, tool in ipairs(player.Backpack:GetChildren()) do
if tool:IsA("Tool") then
tool:Destroy()
end
end
for _, tool in ipairs(character:GetChildren()) do
if tool:IsA("Tool") then
tool:Destroy()
end
end
-- 武器追加
if toolToGive then
local clone = toolToGive:Clone()
clone.Parent = player:WaitForChild("Backpack")
end
end)
SpawnLocation(Blue Team)の子要素のScriptに以下のプログラムを記述します。
local Teams = game:GetService("Teams")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local blueTeam = Teams:FindFirstChild("Blue Team")
local toolToGive = ServerStorage:FindFirstChild("Gun")
script.Parent.Touched:Connect(function(hit)
local character = hit.Parent
local player = Players:GetPlayerFromCharacter(character)
if not player then return end
player.Team = blueTeam
-- 武器削除(全Toolを削除)
for _, tool in ipairs(player.Backpack:GetChildren()) do
if tool:IsA("Tool") then
tool:Destroy()
end
end
for _, tool in ipairs(character:GetChildren()) do
if tool:IsA("Tool") then
tool:Destroy()
end
end
-- 武器追加
if toolToGive then
local clone = toolToGive:Clone()
clone.Parent = player:WaitForChild("Backpack")
end
end)
これでゲームを始めると、冒頭の動画のように、RedTeamになると剣を、BlueTeamになると銃を持つようにできます。
やってみてね~!
コメント