더미가 다시 생겨요
Coddy의 Roblox 게임: 펫 시뮬레이터 코스 레슨. 18개 중 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에서 제거하고, 더미를 파괴한 다음task.delay(Settings.RespawnTime, spawnPile, spot)을 실행합니다.
이 레슨의 더미는 1초 후에 다시 나타납니다. Play를 누른 다음 Check를 누르세요.
직접 해보기
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")
pile:Destroy()
end
-- Meadow의 모든 위치에 더미를 생성합니다
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
spawnPile(spot)
end
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.