Menu
CoddyTech

더미 나누기

Coddy의 Roblox 게임: 펫 시뮬레이터 코스 레슨. 18개 중 3번째.

플레이어는 더미를 클릭해 부숩니다. 파트 안의 ClickDetector는 플레이어가 충분히 가까이 있을 때 클릭한 플레이어와 함께 서버에서 MouseClick을 발생시킵니다.

클릭할 때마다 더미의 Health가 1씩 감소합니다. 값이 0이 되면 더미가 부서집니다. 플레이어는 더미의 Value만큼 코인을 받고, 더미는 파괴됩니다.

local health = pile:GetAttribute("Health") - 1
if health > 0 then
	pile:SetAttribute("Health", health)
	return
end
-- 부서짐

클릭 이벤트는 spawnPile 안에서 연결되며, spawnPile 아래에 작성된 hitPile을 호출합니다. 함수는 이미 선언된 로컬 이름만 사용할 수 있으므로, 먼저 둘 다 위쪽에 local hitPile을 선언한 다음, 나중에 function hitPile(player, pile)로 함수를 작성합니다.

challenge icon

챌린지

쉬움
  1. spawnPile 위에 local hitPile을 선언합니다.
  2. spawnPile에서 ClickDetector를 더미에 추가하고, 해당 ClickDetector의 MouseClick을 hitPile(player, pile)에 연결합니다.
  3. spawnPile 아래에 function hitPile(player, pile)을 작성합니다. Health 속성에서 1을 빼고, 여전히 0보다 크면 값을 저장하고 종료합니다. 그렇지 않으면 더미의 Value를 플레이어의 Coins에 더하고 더미를 삭제합니다.

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")

-- 모든 플레이어의 leaderboard에 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)

-- spot에 놓이는 코인 더미: 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.Parent = piles
	return pile
end

-- Meadow의 모든 spot에 더미를 배치합니다
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
	spawnPile(spot)
end
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

Roblox 게임: 펫 시뮬레이터의 모든 레슨