عملات الصحراء
الدرس 16 من 18 في دورة لعبة Roblox: محاكي الحيوانات الأليفة على Coddy.
للصحراء نقاطها الخاصة، وأكوامها أقوى وأغنى: Settings.DesertHealth وSettings.DesertValue. أنشئ أكوامًا في كل منطقة، وأضف إلى كل منها سمة Zone تحدد موقعها. تعرف النقطة منطقتها: فهي موجودة في Zones.Desert.Spawns، لذا تكون spot.Parent.Parent.Name هي "Desert".
لا تُفتح البوابة إلا على شاشة اللاعب، ويمكن للمخترق فتح أي جدار على شاشته. لذا يضيف فحص الخادم Hit قاعدة أخرى: يتجاهل كومة الصحراء اللاعبين الذين لا يملكون سمة Desert. يفتح العميل الأبواب؛ ويحافظ الخادم على القواعد.
التحدي
صعب- في
spawnPile، استخدم صحة الصحراء وقيمتها للأماكن الموجودة في الصحراء، واضبط سمةZoneلكل كومة على اسم منطقتها. - أنشئ أكوامًا في أماكن كل منطقة في
workspace.Zones. - في معالج
Hit، تجاهل كومة الصحراء إذا لم تكن سمةDesertمضبوطة لدى اللاعب.
اضغط على Play، ثم Check.
جرّب بنفسك
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
local petParts = workspace.PetParts
local pileSpot = {} -- كل كومة -> الموضع الذي تقف عليه
local lastHit = {} -- كل لاعب -> وقت آخر ضربة وجّهها
print("A Meadow pile gives " .. Settings.PileValue .. " coins")
-- Settings.Pets بحسب الاسم
local petInfo = {}
for _, info in Settings.Pets do
petInfo[info.Name] = info
end
-- حيوان أليف: قيمة StringValue في مجلد Pets الخاص باللاعب، تحتوي على اسمه، مع
-- ندرته وقوته بوصفهما سمتين
local function givePet(player, name, equipped)
local info = petInfo[name]
local pet = Instance.new("StringValue")
pet.Name = "Pet"
pet.Value = name
pet:SetAttribute("Rarity", info.Rarity)
pet:SetAttribute("Power", info.Power)
pet:SetAttribute("Equipped", equipped)
pet.Parent = player.Pets
return pet
end
-- عدد الحيوانات الأليفة التي أخرجها اللاعب
local function equippedCount(player)
local count = 0
for _, pet in player.Pets:GetChildren() do
if pet:GetAttribute("Equipped") then
count += 1
end
end
return count
end
-- مقدار ما تضيفه الحيوانات الأليفة للاعب إلى كل ضربة
local function petPower(player)
local power = 0
for _, pet in player.Pets:GetChildren() do
if pet:GetAttribute("Equipped") then
power += pet:GetAttribute("Power")
end
end
return power
end
-- يحصل كل لاعب على 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
-- الحيوانات الأليفة التي يملكها اللاعب
local pets = Instance.new("Folder")
pets.Name = "Pets"
pets.Parent = player
end)
-- كومة عملات في موضع: نسخة من 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.HealthGui.Label.Text = pile:GetAttribute("Health") .. "/" .. pile:GetAttribute("MaxHealth")
pileSpot[pile] = spot
pile.Parent = piles
return pile
end
-- تُنقص الضربة 1، إضافةً إلى قوة الحيوانات الأليفة للاعب، من صحة الكومة؛
-- عندما تصل صحة الكومة إلى 0، تتفكك وتمنح قيمتها
local function hitPile(player, pile)
local health = pile:GetAttribute("Health") - (1 + petPower(player))
if health > 0 then
pile:SetAttribute("Health", health)
pile.HealthGui.Label.Text = health .. "/" .. pile:GetAttribute("MaxHealth")
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
-- يطلب العميل ضرب كومة. لا تثق بالعميل أبدًا: يمكن لأي شخص إطلاق
-- هذا الحدث البعيد بأي بيانات، من أي مكان، وبالسرعة التي يريدها.
remotes.Hit.OnServerEvent:Connect(function(player, pile)
-- كومة حقيقية
if typeof(pile) ~= "Instance" or pile.Parent ~= piles then
return
end
-- ضمن نطاق الوصول
local character = player.Character
local root = character and character:FindFirstChild("HumanoidRootPart")
if not root or (root.Position - pile.Position).Magnitude > Settings.Reach then
return
end
-- ليس بسرعة مفرطة
local now = time()
if lastHit[player] and now - lastHit[player] < Settings.HitCooldown then
return
end
lastHit[player] = now
hitPile(player, pile)
end)
-- اختيار عشوائي موزون: قيمة Chance لكل حيوان أليف (من أصل 100) هي حصته من النطاق
local function rollPet()
local roll = math.random() * 100
for _, info in Settings.Pets do
roll -= info.Chance
if roll <= 0 then
return info.Name
end
end
return Settings.Pets[1].Name
end
-- الفقس: مطالبة التفاعل مع البيضة، وتُدفع تكلفتها بالعملات
workspace.Egg.ProximityPrompt.Triggered:Connect(function(player)
local coins = player.leaderstats.Coins
if coins.Value < Settings.EggPrice then
return
end
coins.Value -= Settings.EggPrice
local name = rollPet()
-- يخرج فورًا إذا كان هناك متسع
local pet = givePet(player, name, equippedCount(player) < Settings.MaxEquipped)
print(player.Name .. " hatched a " .. name)
end)
-- تتبع الحيوانات الأليفة المُخرَجة مالكها: لكل منها جزء في العالم، يُوضَع في
-- في صف خلف اللاعب في كل إطار
local petModels = {} -- كل حيوان أليف -> الجزء الذي يمثله في العالم
RunService.Heartbeat:Connect(function()
for _, player in Players:GetPlayers() do
local root = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
local pets = player:FindFirstChild("Pets")
local slot = 0
for _, pet in pets and pets:GetChildren() or {} do
local model = petModels[pet]
if pet:GetAttribute("Equipped") and root then
slot += 1
if not model then
model = ServerStorage.PetModels[pet.Value]:Clone()
model.Parent = petParts
petModels[pet] = model
end
model.CFrame = root.CFrame * CFrame.new((slot - 2) * 3, -1.5, 4)
elseif model then
model:Destroy()
petModels[pet] = nil
end
end
end
end)
-- يطلب العميل إخراج حيوان أليف أو إعادته. يتحقق الخادم من أن الحيوان الأليف
-- مملوك للاعب، وأن عدد الحيوانات الأليفة الموجودة في الخارج لا يتجاوز MaxEquipped.
remotes.Equip.OnServerEvent:Connect(function(player, pet)
if typeof(pet) ~= "Instance" or pet.Parent ~= player.Pets then
return
end
if pet:GetAttribute("Equipped") then
pet:SetAttribute("Equipped", false)
elseif equippedCount(player) < Settings.MaxEquipped then
pet:SetAttribute("Equipped", true)
end
end)
-- بوابة Desert: ادفع مرة واحدة فتُفتح لذلك اللاعب وحده
workspace.Gate.ProximityPrompt.Triggered:Connect(function(player)
local coins = player.leaderstats.Coins
if player:GetAttribute("Desert") or coins.Value < Settings.GatePrice then
return
end
coins.Value -= Settings.GatePrice
player:SetAttribute("Desert", true)
end)
يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.