화면 속 코인
Coddy의 Roblox 게임: 펫 시뮬레이터 코스 레슨. 20개 중 5번째.
이제 클라이언트입니다. PetClient는 HUD 안에 있는 LocalScript입니다. HUD는 Roblox가 플레이어가 스폰될 때 StarterGui에서 각 플레이어의 화면으로 복사하는 ScreenGui입니다. LocalScript는 해당 플레이어의 기기에서 실행되며, 자신이 실행되는 플레이어의 이름을 가져올 수 있습니다.
local me = game:GetService("Players").LocalPlayer서버 Script에서 LocalPlayer는 nil입니다. 서버는 모든 플레이어를 한 번에 처리합니다.
서버는 플레이어가 참가한 직후 leaderstats를 플레이어에 추가하므로, 클라이언트는 WaitForChild로 이를 기다립니다. 그런 다음 HUD의 Coins 레이블에 코인을 표시하고, 코인이 변경될 때마다 다시 표시합니다. IntValue의 Changed는 새 값과 함께 발생합니다.
레이블에는 서버가 결정한 내용만 표시됩니다. LocalScript에서 Coins.Value를 변경하면 이 플레이어에게 보이는 내용만 바뀌고, 서버에서는 아무것도 바뀌지 않습니다.
챌린지
중급플레이어의 화면에 코인을 표시하세요.
PetClient에서:local Players = game:GetService("Players"),local me = Players.LocalPlayer,local hud = script.Parent.local coins = me:WaitForChild("leaderstats"):WaitForChild("Coins")showCoins()를 작성하세요:hud.Coins.Text = "Coins: " .. coins.Value. 한 번 호출하고coins.Changed에 연결하세요.
‘플레이’를 누르고 더미를 부수세요. 왼쪽 위에 Coins: 5라고 표시됩니다. 그런 다음 ‘확인’을 누르세요.
직접 해보기
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local piles = workspace.Piles
local pileSpot = {} -- 각 pile -> 그 pile이 놓인 spot
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)
-- pile을 클릭할 때는 spawnPile 아래에 작성된 hitPile이 필요하므로, 먼저 이름을 선언합니다
-- 여기서, 둘 중 어느 것도 정의하기 전에
local hitPile
-- 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을 공격합니다
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
-- 한 번 공격하면 pile의 체력이 1 감소합니다;
-- 체력이 0이 되면 pile이 부서지고 그 가치만큼 코인을 지급합니다
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()
-- 몇 초 후 같은 spot에 새로운 pile이 생성됩니다
task.delay(Settings.RespawnTime, spawnPile, spot)
end
-- Meadow의 모든 spot에 pile을 하나씩 생성합니다
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
spawnPile(spot)
end
Roblox 게임: 펫 시뮬레이터의 모든 레슨
직접 연습해 보세요: Roblox 플레이그라운드