Menu
CoddyTech

أخرج ثلاثة حيوانات أليفة

الدرس 12 من 20 في دورة لعبة Roblox: محاكي الحيوانات الأليفة على Coddy.

يجب أن تصطف الحيوانات الأليفة، ولا يمكن أن يكون خارجًا في الوقت نفسه سوى Settings.MaxEquipped (3). لا يخرج حيوان أليف جديد إلا عندما يكون عدد الحيوانات الموجودة خارجًا أقل من ذلك؛ أما البقية فتنتظر في مجلد Pets.

يتم عدّ الحيوانات الأليفة الموجودة خارجًا باستخدام حلقة تمر على المجلد:

local count = 0
for _, pet in player.Pets:GetChildren() do
	if pet:GetAttribute("Equipped") then
		count += 1
	end
end

لجعلها تقف في صف، رقّم الحيوانات الأليفة الموجودة خارجًا أثناء وضعها: 1 و2 و3، ثم حوّل الرقم إلى إزاحة جانبية: باستخدام (slot - 2) * 5، ينتقل الموضع 1 بمقدار 5 وحدات إلى اليسار، والموضع 3 بمقدار 5 إلى اليمين.

challenge icon

التحدي

متوسط

أخرج ما يصل إلى ثلاثة حيوانات أليفة في صف خلفك.

  1. اكتب equippedCount(player) لحساب عدد الحيوانات الأليفة لدى اللاعب المجهّزة.
  2. فقّس باستخدام givePet(player, name, equippedCount(player) < Settings.MaxEquipped).
  3. في Heartbeat، عدّ slot بالقيم 1 و2 و3 للحيوانات الأليفة التي أخرجتها، وضع كلًّا منها عند root.CFrame * CFrame.new((slot - 2) * 3, -1.5, 4).

اضغط «العب» وفقّس أربعة حيوانات: ستمشي ثلاثة منها خلفك جنبًا إلى جنب. ثم اضغط «تحقق».

جرّب بنفسك

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

-- الدرس 12: يوضع Three Pets Out هنا

-- يحصل كل لاعب على 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, true)
	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")
		for _, pet in pets and pets:GetChildren() or {} do
			local model = petModels[pet]
			if pet:GetAttribute("Equipped") and root then
				if not model then
					model = ServerStorage.PetModels[pet.Value]:Clone()
					model.Parent = petParts
					petModels[pet] = model
				end
				model.CFrame = root.CFrame * CFrame.new(0, -1.5, 4)
			end
		end
	end
end)

جميع دروس لعبة Roblox: محاكي الحيوانات الأليفة

تدرّب بنفسك: Playground لـ Roblox