Menu
CoddyTech

プレイヤーをテレポートする

Coddyの「Robloxゲーム:災害サバイバル」コースのレッスン 6/18。

誰も立っていない島ではゲームになりません。ラウンドが始まると、すべてのプレイヤーがマップのSpawnに移動し、ラウンドが終わるとロビーに戻ります。

Players:GetPlayers()はゲーム内の全員を一覧表示し、プレイヤーの体はModelであるplayer.Characterです。Model:PivotTo(cframe)はモデル全体を一度に移動し、各パーツの位置関係を保ちます。CFrameは位置と回転を組み合わせたもので、Vector3を加えると移動できます:

local function teleport(player, target)
	local character = player.Character
	if character then
		character:PivotTo(target.CFrame + Vector3.new(0, 4, 0))
	end
end

上方向に4スタッド移動させることで、プレイヤーをターゲットの中ではなく、少し上に出現させます。参加直後や死亡直後はplayer.Characterが一時的にnilになるため、ifを使います。

challenge icon

チャレンジ

中級

プレイヤーを移動させましょう:

  1. 上部付近で、ロビーのスポーン地点を保持します: local lobbySpawn = workspace.LobbySpawn。
  2. レッスンと同じように teleport(player, target) を記述します。
  3. マップの読み込みが完了した直後に、すべてのプレイヤーを map.Spawn にテレポートさせます。
  4. ラウンドのカウントダウンが終了したら、マップが破棄される前に、すべてのプレイヤーを lobbySpawn にテレポートさせます。

Play を押して一緒に動きを確認し、その後 Check を押します。

自分で試してみよう

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

print("Rounds last " .. Settings.RoundLength .. " seconds")

-- すべてのプレイヤーに、リーダーボード上の Wins が付与される
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
end)

-- ロビーのステータスボード
local gui = Instance.new("SurfaceGui")
gui.Parent = workspace.StatusBoard

local status = Instance.new("TextLabel")
status.Name = "Status"
status.Size = UDim2.fromScale(1, 1)
status.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
status.TextColor3 = Color3.fromRGB(255, 255, 255)
status.TextScaled = true
status.Text = "Welcome to Disaster Survival!"
status.Parent = gui

local function setStatus(text)
	status.Text = text
end

-- ラウンドのループ
while true do
	for seconds = Settings.Intermission, 1, -1 do
		setStatus("Intermission: " .. seconds)
		task.wait(1)
	end

	local map = ServerStorage.Maps.Island:Clone()
	map.Name = "Map"
	map.Parent = workspace

	for seconds = Settings.RoundLength, 1, -1 do
		setStatus("Time left: " .. seconds)
		task.wait(1)
	end

	map:Destroy()
end
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

Robloxゲーム:災害サバイバルのすべてのレッスン