レア度抽選
Coddyの「Robloxゲーム:ペットシミュレーター」コースのレッスン 10/20。
卵から必ずしも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"])を使うと、すばやく見つけられます。
チャレンジ
中級各ペットのChanceに基づいて、孵化するペットを抽選します。
- Nameをキーにした
Settings.PetsであるpetInfoを作成します。 givePetで、petInfo[name]からRarity属性とPower属性を設定します。- 重み付き抽選を行う
rollPet()を書き、Catの代わりにrollPet()で孵化させます。printには孵化したペットの名前を表示します。
「プレイ」を押して何匹か孵化させます。Outputに、ときどき別のペットの名前が表示されます。その後、「確認」を押します。
自分で試してみよう
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のすべての場所に山を配置する
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)
-- レッスン 10: レア度の抽選をここに追加
-- 孵化: 卵のプロンプトで、コインを支払う
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)
Robloxゲーム:ペットシミュレーターのすべてのレッスン
自分で練習してみよう: Robloxプレイグラウンド