حيوانك الأليف يتبعك
الدرس 11 من 18 في دورة لعبة Roblox: محاكي الحيوانات الأليفة على Coddy.
ينبغي أن تتبعك الحيوانات الأليفة. يظهر حيوان أليف جديد فورًا إذا كان عدد الحيوانات الموجودة أقل من Settings.MaxEquipped (3)، ويُحدَّد بوسمة Equipped.
يحصل كل حيوان أليف موجود على جزء في العالم، وهو نسخة من نموذجه من ServerStorage.PetModels، وفي كل إطار يضعه الخادم خلف مالكه. تُطلِق RunService.Heartbeat حدثًا مرة واحدة في كل إطار، ويؤدي ضرب قيم CFrame إلى وضع إحداها بالنسبة إلى الأخرى:
model.CFrame = root.CFrame * CFrame.new(0, -1.5, 4) -- على بُعد 4 وحدات studs خلفه، وأخفض قليلًايتذكّر جدول أي جزء يتبع أي حيوان أليف، لذا يفقد الحيوان الأليف الذي يُعاد إلى مكانه جزأه.
التحدي
متوسط- اكتب
equippedCount(player). أضف إلىgivePetوسيطًا ثالثًا هوequipped، وخزّنه كسمةEquipped، واجعل الفقس يستخدمequippedCount(player) < Settings.MaxEquipped. - احصل على
RunServiceوlocal petParts = workspace.PetParts. عند Heartbeat، ولكل لاعب ولكل حيوان أليف: إذا كان يرافق اللاعب (وكان لدى اللاعب شخصية)، فتأكد من أن له جزءًا (نسخة منServerStorage.PetModels[pet.Value]داخل petParts) وضعه عندroot.CFrame * CFrame.new((slot - 2) * 3, -1.5, 4)، مع ترقيم الخانات 1 و2 و3؛ وإذا أُعيد إلى مكانه، فدمّر جزأه.
اضغط Play، وافقس، ثم تجوّل. بعد ذلك اضغط Check.
جرّب بنفسك
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
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)
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.Parent = player.Pets
return pet
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
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()
givePet(player, name)
print(player.Name .. " hatched a " .. name)
end)
يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.
جميع دروس لعبة Roblox: محاكي الحيوانات الأليفة
3البيض والحيوانات الأليفة
فقّس بيضة