Menu
CoddyTech

التحميل عند الانضمام

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

والآن اقرأ البيانات المحفوظة عند انضمام اللاعب. تُعيد GetAsync ما خزّنته SetAsync تحت المفتاح، أو nil للاعب الذي لم يسبق له الحفظ. وقد تفشل أيضًا، لذا استدعِها داخل pcall، التي تُعيد ما إذا كانت قد نجحت ثم القيمة:

local ok, saved = pcall(function()
	return saveStore:GetAsync(player.UserId)
end)
if ok and saved then
	print(saved.Coins)
end

حمّل البيانات بعد إنشاء المجلدات، حتى يكون لدى الحيوانات الأليفة مجلد Pets لتوضع فيه. تخطَّ أي اسم محفوظ غير موجود في petInfo: ينبغي ألا يتسبب حيوان أليف أُزيل من اللعبة في تعطيل عملية الانضمام.

challenge icon

التحدي

صعب

أعِد إلى اللاعبين ما حفظوه.

  1. في PlayerAdded، بعد إنشاء مجلد Pets: local ok, saved = pcall(function() return saveStore:GetAsync(player.UserId) end).
  2. إذا كان ok and saved: عيّن Coins إلى saved.Coins وسمة Desert إلى saved.Desert.
  3. لكل حيوان أليف محفوظ في petInfo، نفّذ givePet(player, pet.Name, pet.Equipped == true).

اضغط «العب»، وفقّس بيضة، ثم اضغط «إيقاف». بعد ذلك اضغط «العب» مجددًا: ستعود عملاتك وحيواناتك الأليفة. ثم اضغط «تحقق».

جرّب بنفسك

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local Settings = require(game.ReplicatedStorage.Settings)

local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
local petParts = workspace.PetParts
local saveStore = DataStoreService:GetDataStore("Pets")
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
	-- الدرس 20: يوضع تحميل البيانات عند الانضمام هنا
end)

-- كومة عملات في موضع: نسخة من ServerStorage.CoinPile تحمل صحتها
-- وقيمتها كسمات
local function spawnPile(spot)
	local desert = spot.Parent.Parent.Name == "Desert"
	local health = desert and Settings.DesertHealth or Settings.PileHealth
	local pile = ServerStorage.CoinPile:Clone()
	pile.Position = spot.Position
	pile:SetAttribute("MaxHealth", health)
	pile:SetAttribute("Health", health)
	pile:SetAttribute("Value", desert and Settings.DesertValue or Settings.PileValue)
	pile:SetAttribute("Zone", spot.Parent.Parent.Name)
	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

-- كومة في كل موقع، في كل منطقة
for _, zone in workspace.Zones:GetChildren() do
	for _, spot in zone.Spawns:GetChildren() do
		spawnPile(spot)
	end
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
	-- في منطقة فتحها اللاعب
	if pile:GetAttribute("Zone") == "Desert" and not player:GetAttribute("Desert") then
		return
	end
	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)
	-- يسمع الجميع عن أندر الحيوانات الأليفة
	if pet:GetAttribute("Rarity") == "Legendary" then
		remotes.Announce:FireAllClients(player.Name, name)
	end
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)

-- الحفظ: Coins وDesert وكل حيوان أليف
local function save(player)
	local pets = {}
	for _, pet in player.Pets:GetChildren() do
		table.insert(pets, { Name = pet.Value, Equipped = pet:GetAttribute("Equipped") == true })
	end
	local data = {
		Coins = player.leaderstats.Coins.Value,
		Desert = player:GetAttribute("Desert") == true,
		Pets = pets,
	}
	pcall(function()
		saveStore:SetAsync(player.UserId, data)
	end)
end

Players.PlayerRemoving:Connect(save)

game:BindToClose(function()
	for _, player in Players:GetPlayers() do
		save(player)
	end
end)
quiz iconاختبر نفسك

يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.

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

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