희귀도 뽑기
Coddy의 Roblox 게임: 펫 시뮬레이터 코스 레슨. 20개 중 10번째.
알이 항상 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"])을 사용하면 빠르게 찾을 수 있습니다.
챌린지
중급각 반려동물의 확률에 따라 부화할 반려동물을 추첨하세요.
petInfo를 만드세요.Settings.Pets를 Name을 키로 하는 테이블로 만듭니다.givePet에서petInfo[name]의Rarity와Power특성을 설정하세요.- 가중 추첨 함수인
rollPet()를 작성하고, Cat 대신rollPet()으로 부화시키세요. 출력에는 부화한 반려동물의 이름이 표시됩니다.
‘플레이’를 누르고 몇 번 부화해 보세요. 이제 가끔 출력에 다른 반려동물의 이름이 표시됩니다. 그런 다음 ‘확인’을 누르세요.
직접 해보기
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
-- 클라이언트가 더미를 타격하겠다고 요청합니다. 클라이언트를 절대 신뢰하지 마세요. 누구나 호출할 수 있습니다
-- 누구나 어디서든 원하는 만큼 빠르게 이 remote에 무엇이든 보낼 수 있습니다.
remotes.Hit.OnServerEvent:Connect(function(player, pile)
-- 실제 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)
-- Lesson 10: Rarity Rolls는 여기에 들어갑니다
-- 부화: 코인으로 비용을 지불하는 알의 prompt
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 플레이그라운드