العملات على الشاشة
الدرس 5 من 18 في دورة لعبة Roblox: محاكي الحيوانات الأليفة على Coddy.
الآن ننتقل إلى العميل. إن PetClient هو LocalScript داخل HUD، وهو ScreenGui ينسخه Roblox من StarterGui إلى شاشة كل لاعب عند دخوله إلى اللعبة. يعمل LocalScript على جهاز اللاعب نفسه، ويمكنه تحديد اللاعب الذي يعمل من أجله:
local me = game:GetService("Players").LocalPlayerيضع الخادم leaderstats داخل اللاعب بعد لحظات من انضمامه، لذا ينتظره العميل باستخدام WaitForChild. ثم يعرض العملات في تسمية Coins ضمن HUD، ويحدّثها مجددًا كلما تغيّرت: يُطلق Changed الخاص بـ IntValue مع القيمة الجديدة.
تعرض التسمية فقط ما قرره الخادم. إن تغيير Coins.Value في LocalScript سيغيّر ما يراه هذا اللاعب فحسب، ولن يغيّر شيئًا على الخادم.
التحدي
متوسطفي PetClient:
- احصل على
Playersوlocal me = Players.LocalPlayerوlocal hud = script.Parent. - انتظر
leaderstatsوCoinsالخاصة بها. - اكتب
showCoins()، التي تضبطhud.Coins.TextعلىCoins: 5(القيمة)، واستدعها مرة واحدة، ثم اربطها بـ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)
-- يتطلب النقر على الكومة hitPile، المكتوبة أسفل spawnPile: صرّح عنها
-- هنا، قبل تعريف أيٍّ منهما
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
يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.