희귀도 굴리기
Coddy의 Roblox 게임: 펫 시뮬레이터 코스 레슨. 18개 중 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의 값으로 펫의Rarity및Power속성을 설정하세요.- 가중치 추첨인
rollPet()을 작성하고, Cat 대신rollPet()으로 부화시키세요(출력에는 부화한 이름이 표시됩니다).
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 = {} -- 각 pile -> 해당 pile이 놓인 spot
local lastHit = {} -- 각 player -> 마지막으로 타격한 시각
print("A Meadow pile gives " .. Settings.PileValue .. " coins")
-- 펫: player의 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
-- 모든 player의 리더보드에 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)
-- spot에 놓인 코인 pile: 체력을 속성으로 가진 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
-- 한 번 타격할 때마다 pile의 체력이 1 감소함;
-- 체력이 0이 되면 pile이 부서지고 해당 값만큼 코인을 지급함
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()
-- 몇 초 후 같은 spot에 새 pile 생성
task.delay(Settings.RespawnTime, spawnPile, spot)
end
-- Meadow의 모든 spot에 pile 하나씩 생성
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
spawnPile(spot)
end
-- 클라이언트가 pile 타격을 요청함. 클라이언트를 절대 신뢰하지 말 것: 누구나 이 remote를 호출할 수 있음
-- 이 원격 이벤트를 어디서든 어떤 값으로든 원하는 만큼 빠르게 호출할 수 있습니다.
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)
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.