Menu
CoddyTech

レア度の抽選

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

卵から必ずしもCatが出るとは限りません。Settings.Petsには、100を基準とするChance(Cat 45、Dog 30、Bunny 15、Fox 8、Dragon 2)、レア度、パワーとともに、すべてのペットが一覧表示されています。

重み付き抽選:0から100までの数値を選び、リストを上から順にたどって、各ペットの確率をその数値から差し引きます。数値が0以下になったペットが当選します。そのため、確率45のペットは平均して100回中45回当選します。

local roll = math.random() * 100
for _, info in Settings.Pets do
	roll -= info.Chance
	if roll <= 0 then
		return info.Name
	end
end

孵化したペットは、属性としてRarityとPowerも持ちます。名前でペットを検索するテーブル(petInfo["Fox"])を使うと、すばやく見つけられます。

challenge icon

チャレンジ

中級
  1. petInfoを作成し、Settings.PetsをNameをキーとして参照できるようにします。
  2. givePetで、petInfoからペットのRarity属性とPower属性を設定します。
  3. 重み付き抽選のrollPet()を書き、CatではなくrollPet()で孵化させます(printは孵化したペットの名前を表示します)。

Playを押して、何個か孵化させます。次にCheckを押します。チェックでは抽選結果が確率どおりになるか確認するため、確率が変更されます。

自分で試してみよう

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

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

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

-- ペット:プレイヤーのPetsフォルダー内にある、名前を保持するStringValue
local function givePet(player, name)
	local pet = Instance.new("StringValue")
	pet.Name = "Pet"
	pet.Value = name
	pet.Parent = player.Pets
	return pet
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
	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)

-- 孵化:卵のプロンプトで、コインを支払う
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 = "Cat"
	givePet(player, name)
	print(player.Name .. " hatched a " .. name)
end)
quiz icon腕試し

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

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