화면 속 코인
Coddy의 Roblox 게임: 펫 시뮬레이터 코스 레슨. 18개 중 5번째.
이제 클라이언트입니다. PetClient는 HUD 안에 있는 LocalScript입니다. HUD는 Roblox가 플레이어가 생성될 때 StarterGui에서 각 플레이어의 화면으로 복사하는 ScreenGui입니다. LocalScript는 해당 플레이어의 기기에서 실행되며, 자신이 실행되는 플레이어를 지정할 수 있습니다.
local me = game:GetService("Players").LocalPlayer서버는 플레이어가 참가한 직후 leaderstats를 플레이어에 추가하므로 클라이언트는 WaitForChild를 사용해 기다립니다. 그런 다음 HUD의 Coins 레이블에 코인을 표시하고, 코인이 바뀔 때마다 다시 표시합니다. IntValue의 Changed는 새 값과 함께 발생합니다.
레이블에는 서버가 결정한 내용만 표시됩니다. LocalScript에서 Coins.Value를 변경하면 해당 플레이어의 화면에만 반영되고 서버에는 아무 영향도 주지 않습니다.
챌린지
중급PetClient에서:
Players,local me = Players.LocalPlayer,local hud = script.Parent를 가져옵니다.leaderstats와 그 안의Coins가 준비될 때까지 기다립니다.hud.Coins.Text를Coins: 5(값)로 설정하는showCoins()를 작성하고, 한 번 호출한 다음Coins.Changed에 연결합니다.
Play를 눌러 코인 더미를 부숩니다. 그런 다음 Check를 누릅니다.
직접 해보기
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local piles = workspace.Piles
local pileSpot = {} -- 각 더미 -> 더미가 놓인 자리
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
pileSpot[pile] = spot
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")
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
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.