더미가 다시 나타나요
Coddy의 Roblox 게임: 펫 시뮬레이터 코스 레슨. 20개 중 4번째.
모든 더미를 부수면 게임에 코인이 더 이상 남지 않습니다. 부서진 더미는 Settings.RespawnTime초 후 원래 위치에 다시 나타나야 합니다.
더미는 자기 위치를 기억해야 합니다. 더미를 키로 사용하는 테이블이 그 역할을 합니다.
local pileSpot = {} -- 각 pile -> 해당 pile이 놓인 spot
pileSpot[pile] = spottask.delay(seconds, fn, ...)는 지금 어떤 작업도 중단하지 않고 인수와 함께 나중에 함수를 호출합니다.
task.delay(Settings.RespawnTime, spawnPile, spot)더미가 부서지면 테이블에서 해당 더미를 제거하세요. 그래야 테이블에 지금까지 존재했던 모든 더미가 계속 남지 않습니다.
챌린지
쉬움부서진 더미가 원래 있던 자리에 다시 생기도록 하세요.
- 맨 위쪽에:
local pileSpot = {}. spawnPile에서pileSpot[pile] = spot을 저장하세요.hitPile에서 더미가 부서질 때, 더미의 자리를 읽고pileSpot[pile] = nil로 설정한 다음 더미를 파괴하고, 이어서task.delay(Settings.RespawnTime, spawnPile, spot)을 실행하세요.
‘플레이’를 눌러 더미를 부수세요. 3초 후 원래 자리에 새 더미가 생깁니다. 그런 다음 ‘확인’을 누르세요.
직접 해보기
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local piles = workspace.Piles
print("A Meadow pile gives " .. Settings.PileValue .. " coins")
-- 모든 플레이어에게 리더보드의 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
end)
-- 더미를 클릭할 때는 spawnPile 아래에 작성된 hitPile이 필요하므로, 먼저 이름을 선언합니다
-- 여기서 두 함수보다 앞에 선언합니다
local hitPile
-- 지점에 놓이는 코인 더미: 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)
-- 클릭하면 더미를 칩니다
local detector = Instance.new("ClickDetector")
detector.Parent = pile
pile.Parent = piles
detector.MouseClick:Connect(function(player)
hitPile(player, pile)
end)
return pile
end
-- 한 번 칠 때마다 더미의 체력이 1 줄어듭니다;
-- 체력이 0이 되면 더미가 부서지고 가치만큼 코인을 지급합니다
function hitPile(player, pile)
local health = pile:GetAttribute("Health") - 1
if health > 0 then
pile:SetAttribute("Health", health)
return
end
player.leaderstats.Coins.Value += pile:GetAttribute("Value")
-- Lesson 4: Piles Come Back은 여기에 들어갑니다
pile:Destroy()
end
-- Meadow의 모든 지점에 더미를 놓습니다
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
spawnPile(spot)
end
Roblox 게임: 펫 시뮬레이터의 모든 레슨
직접 연습해 보세요: Roblox 플레이그라운드