Menu
CoddyTech

砂漠の門

Coddyの「Robloxゲーム:ペットシミュレーター」コースのレッスン 16/20。

奥にある赤い壁は砂漠へのゲートで、「解放」プロンプトが表示されています。Settings.GatePrice分のコインを支払うと開くはずですが、開くのは支払ったプレイヤーだけです。

これには両方の側の処理が必要です。サーバーはコインを受け取り、プレイヤーの属性として解放状態を記録します。この属性はDesertで、どのルールからでも確認できます。クライアントはその属性を監視し、ゲートを自分の画面上で開きます。Robloxでは、各プレイヤーのデバイスがそのプレイヤー自身のキャラクターを動かすため、LocalScriptによって非衝突化された壁はそのプレイヤーを通しますが、ほかのプレイヤーにとってはそのまま衝突します。

-- クライアント
workspace.Gate.CanCollide = false
workspace.Gate.Transparency = 0.7

同じ方法でゲートのプロンプトを非表示にすると、2回目の解放を促さずに済みます。

challenge icon

チャレンジ

難しい

支払いをしたプレイヤーのために、Desertのゲートを開きます。

  1. PetServerのworkspace.Gate.ProximityPrompt.Triggeredで、プレイヤーにDesert属性があるか、コインがSettings.GatePriceより少ない場合は処理を止めます。それ以外の場合は料金を差し引き、Desertをtrueに設定します。
  2. PetClientのopenGate()では、me:GetAttribute("Desert")の場合、ゲートのCanCollideをfalse、Transparencyを0.7に設定します。今すぐ実行し、Desertが変更されたときにも実行します。

「プレイ」を押して、コインを100枚獲得し、ロックを解除します。壁が薄くなります。その後、「確認」を押します。

自分で試してみよう

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

local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
local petParts = workspace.PetParts
local pileSpot = {} -- 各コインの山 -> その山が置かれている場所
local lastHit = {} -- 各プレイヤー -> 最後に叩いた時刻

print("A Meadow pile gives " .. Settings.PileValue .. " coins")

-- Settings.Petsを名前で参照
local petInfo = {}
for _, info in Settings.Pets do
	petInfo[info.Name] = info
end

-- ペット:プレイヤーのPetsフォルダにある、名前を保持するStringValueで、
-- レア度とパワーを属性として持つ
local function givePet(player, name, equipped)
	local info = petInfo[name]
	local pet = Instance.new("StringValue")
	pet.Name = "Pet"
	pet.Value = name
	pet:SetAttribute("Rarity", info.Rarity)
	pet:SetAttribute("Power", info.Power)
	pet:SetAttribute("Equipped", equipped)
	pet.Parent = player.Pets
	return pet
end

-- プレイヤーが出しているペットの数
local function equippedCount(player)
	local count = 0
	for _, pet in player.Pets:GetChildren() do
		if pet:GetAttribute("Equipped") then
			count += 1
		end
	end
	return count
end

-- プレイヤーのペットが各攻撃に加えるパワー
local function petPower(player)
	local power = 0
	for _, pet in player.Pets:GetChildren() do
		if pet:GetAttribute("Equipped") then
			power += pet:GetAttribute("Power")
		end
	end
	return power
end

-- すべてのプレイヤーのリーダーボードにCoinsを追加する
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats

	-- プレイヤーが所有するペット
	local pets = Instance.new("Folder")
	pets.Name = "Pets"
	pets.Parent = player
end)

-- 場所に置かれたコインの山:ServerStorage.CoinPileのコピーで、体力
-- と価値を属性として持つ
local function spawnPile(spot)
	local pile = ServerStorage.CoinPile:Clone()
	pile.Position = spot.Position
	pile:SetAttribute("MaxHealth", Settings.PileHealth)
	pile:SetAttribute("Health", Settings.PileHealth)
	pile:SetAttribute("Value", Settings.PileValue)
	pile.HealthGui.Label.Text = pile:GetAttribute("Health") .. "/" .. pile:GetAttribute("MaxHealth")
	pileSpot[pile] = spot
	pile.Parent = piles
	return pile
end

-- 叩くと、山の体力が1とプレイヤーのペットのパワーの合計だけ減る;
-- 0になると山は壊れ、その価値に相当するコインが支払われる
local function hitPile(player, pile)
	local health = pile:GetAttribute("Health") - (1 + petPower(player))
	if health > 0 then
		pile:SetAttribute("Health", health)
		pile.HealthGui.Label.Text = health .. "/" .. pile:GetAttribute("MaxHealth")
		return
	end
	player.leaderstats.Coins.Value += pile:GetAttribute("Value")
	local spot = pileSpot[pile]
	pileSpot[pile] = nil
	pile:Destroy()
	-- 数秒後、同じ場所に新しい山が現れる
	task.delay(Settings.RespawnTime, spawnPile, spot)
end

-- Meadowの各スポットに山を1つずつ配置する
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
	spawnPile(spot)
end

-- クライアントが山への攻撃を要求する。クライアントを決して信用してはいけない。誰でも
-- このリモートイベントを、どこからでも、任意の内容で、好きなだけ速く発火できる。
remotes.Hit.OnServerEvent:Connect(function(player, pile)
	-- 実在する山か
	if typeof(pile) ~= "Instance" or pile.Parent ~= piles then
		return
	end
	-- 届く範囲内か
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or (root.Position - pile.Position).Magnitude > Settings.Reach then
		return
	end
	-- 速すぎないか
	local now = time()
	if lastHit[player] and now - lastHit[player] < Settings.HitCooldown then
		return
	end
	lastHit[player] = now
	hitPile(player, pile)
end)

-- 重み付き抽選:各ペットのChance(100のうちの値)が抽選範囲に占める割合になる
local function rollPet()
	local roll = math.random() * 100
	for _, info in Settings.Pets do
		roll -= info.Chance
		if roll <= 0 then
			return info.Name
		end
	end
	return Settings.Pets[1].Name
end

-- 孵化:卵のプロンプトで、コインを支払って行う
workspace.Egg.ProximityPrompt.Triggered:Connect(function(player)
	local coins = player.leaderstats.Coins
	if coins.Value < Settings.EggPrice then
		return
	end
	coins.Value -= Settings.EggPrice
	local name = rollPet()
	-- 空きがあれば、すぐに外に出す
	local pet = givePet(player, name, equippedCount(player) < Settings.MaxEquipped)
	print(player.Name .. " hatched a " .. name)
end)

-- 外に出ているペットは飼い主についていく。各ペットにはワールド内に対応するパーツがあり、
-- 毎フレーム、プレイヤーの後ろに一列に並ぶ
local petModels = {} -- 各ペット -> ワールド内の対応するパーツ
RunService.Heartbeat:Connect(function()
	for _, player in Players:GetPlayers() do
		local root = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
		local pets = player:FindFirstChild("Pets")
		local slot = 0
		for _, pet in pets and pets:GetChildren() or {} do
			local model = petModels[pet]
			if pet:GetAttribute("Equipped") and root then
				slot += 1
				if not model then
					model = ServerStorage.PetModels[pet.Value]:Clone()
					model.Parent = petParts
					petModels[pet] = model
				end
				model.CFrame = root.CFrame * CFrame.new((slot - 2) * 3, -1.5, 4)
			elseif model then
				-- しまう:対応するパーツを削除する
				model:Destroy()
				petModels[pet] = nil
			end
		end
	end
end)

-- クライアントはペットを出すかしまうかを要求する。サーバーは、そのペットが
-- プレイヤー自身のものであり、出している数がMaxEquippedを超えないことを確認する。
remotes.Equip.OnServerEvent:Connect(function(player, pet)
	if typeof(pet) ~= "Instance" or pet.Parent ~= player.Pets then
		return
	end
	if pet:GetAttribute("Equipped") then
		pet:SetAttribute("Equipped", false)
	elseif equippedCount(player) < Settings.MaxEquipped then
		pet:SetAttribute("Equipped", true)
	end
end)

Robloxゲーム:ペットシミュレーターのすべてのレッスン

自分で練習してみよう: Robloxプレイグラウンド