شرح · أنظمة اللعبة

كيف تصنع مخزونًا (Inventory) في Roblox Studio

يبدو المخزون مهمة واجهة، لكن مكان حفظ العناصر هو ما يقرر هل يمكن اختراقه وهل يضيّع العناصر. في هذا الدليل توجد عناصر كل لاعب في جدول على الخادم، ويحصل كل لاعب على نسخة يرسمها شبكةً، ويجهّز الـ Tool ويستخدم العناصر المستهلكة عبر طلبات يتحقق منها الخادم، ويُحفظ كل شيء في مخزن البيانات.

آخر تحديث ٢٠ نوفمبر ٢٠٢٦القراءة في 13 دبقلم

باختصار

احفظ عناصر كل لاعب في جدول على الخادم، داخل ModuleScript في ServerScriptService، ولا يُسمح إلا لهذا الكود بإضافتها وإزالتها وتكديسها. صِف العناصر مرة واحدة في ModuleScript في ReplicatedStorage (الـ id والاسم وأقصى حجم للكومة). بعد كل تغيير يرسل الخادم إلى هذا اللاعب RemoteEvent مع نسخة من مخزونه، ويرسمه LocalScript شبكةً عبر ScrollingFrame وUIGridLayout.

  • العميل لا يضيف العناصر أبدًا. يستطيع فقط أن يطلب، مثل "جهّز سيفي"، والخادم يتحقق من أن اللاعب يملكه قبل أن يفعل أي شيء.
  • العناصر لا تظهر إلا عبر كود على الخادم تتحكم فيه: الالتقاط، والشراء من المتجر بعد التحقق على الخادم، والمكافآت.
  • احفظ معرّفات العناصر وكمياتها، لا الـ Tool، عبر DataStoreService عندما يخرج اللاعب، وعندما يتوقف الخادم، وكل بضع دقائق. ولا تحفظ أبدًا لاعبًا لم تُحمَّل بياناته.
في هذه الصفحة
  1. كيف يعمل
  2. لماذا العناصر على الخادم
  3. 1. تجهيز Explorer
  4. 2. كتالوج العناصر
  5. 3. المخزون على الخادم
  6. 4. التحميل والحفظ
  7. 5. التقاط العناصر
  8. 6. التجهيز والاستخدام
  9. 7. شبكة الواجهة
  10. الاختبار
  11. Common errors
  12. مع RoCode
  13. الأسئلة الشائعة
  14. المصادر والقراءات

كيف يُبنى المخزون

رسم توضيحياضغط B، ومرّر المؤشر فوق عنصر لتقرأ وصفه، وانقر على Sword لتمسكه بيدك.

يتكون النظام من ستة أجزاء صغيرة، لكل منها مهمة واحدة. كل قواعد العناصر تعيش في وحدة واحدة على الخادم، لذا عندما ينكسر شيء تبحث في مكان واحد بالضبط.

أجزاء النظام وأين تعمل
الجزءالنوع والمكانالمهمة
ItemCatalogModuleScript في ReplicatedStorageما هو كل عنصر: الاسم، والوصف، وأقصى حجم للكومة، وهل يمكن تجهيزه أو استخدامه. يقرؤه الخادم وكل العملاء.
InventoryServiceModuleScript في ServerScriptServiceيحفظ خانات كل اللاعبين. الكود الوحيد الذي يضيف العناصر ويزيلها ويكدّسها.
InventoryDataScript في ServerScriptServiceيحمّل عناصر اللاعب عند دخوله ويحفظها.
InventoryActionsScript في ServerScriptServiceيتحقق من طلبات التجهيز والاستخدام وينفذها.
ItemPickupScript داخل Partيعطي العنصر لمن التقطه.
InventoryClientLocalScript في StarterPlayerScriptsيرسم الشبكة ويرسل الطلبات. لا يغيّر شيئًا بنفسه.

البيانات تسير في اتجاه واحد. يغيّر الخادم المخزون، ثم يرسل إلى هذا اللاعب InventoryUpdated مع المخزون كاملًا، فيعيد العميل رسمه. وعندما ينقر اللاعب على خانة، يرسل العميل EquipItem أو UseItem مع الـ id الخاص بالعنصر؛ فيتحقق الخادم من الطلب، ويُجري التغيير، وتصل النسخة الجديدة بالطريق نفسه. إرسال المخزون كاملًا أبسط من إرسال التغييرات، ولبضع عشرات من الخانات تبقى الرسالة صغيرة.

لماذا لا يضيف العميل العناصر أبدًا

من المغري إضافة العنصر مباشرة في الـ LocalScript الذي يعرضه: نقر اللاعب، فامتلأت الخانة. لكن هذا ينكسر بطريقتين.

  • لا أحد غيره يرى ذلك. التغيير في LocalScript موجود فقط على جهاز هذا اللاعب. الخادم لا يعرف به، لذا لا يُحفظ، ولا يستطيع أي سكربت على الخادم استخدامه.
  • أي شخص يستطيع تزويره. إذا أصلحت ذلك بـ remote مثل AddItem(itemId, amount)، فسيستطيع الغشاش استدعاء هذا الحدث بنفسه، بأي معاملات وبأي عدد من المرات. ودليل الأمان من Roblox يقول إن على الخادم التحقق من كل ما يرسله العميل قبل استخدامه.

لذلك في هذا الدليل لا يستطيع العميل إلا أن يطلب فعل شيء بالعناصر التي يملكها اللاعب بالفعل: "جهّز هذا" و"استخدم هذا". في كل طلب id عنصر واحد ولا كميات، ويمر على الخادم بفحص واحد: أنه نص، وأن العنصر موجود، وأن لدى اللاعب واحدًا منه على الأقل، وأن اللاعب لا يرسل أكثر من أربعة طلبات في الثانية. ثم يجد الخادم الـ Tool بنفسه من الكتالوج. ولا يقبل من العميل أبدًا Tool أو مسارًا أو instance، كما يحذّر الدليل نفسه.

لا تدخل العناصر المخزون إلا عبر كود على الخادم: التقاط تحقق منه الخادم، أو شراء خصم الخادم ثمنه (يبنيه دليل المتجر)، أو مكافأة مهمة أو غنيمة. وعن الـ remotes عمومًا، مع التحقق من أنواع أخرى من المعاملات، اقرأ دليل RemoteEvent.

الخطوة 1: جهّز Explorer

أنشئ هذه الكائنات يدويًا. السكربتات في الخطوات التالية تبحث عنها بالاسم، لذا اكتب كل اسم كما هو هنا بالضبط.

  1. في ReplicatedStorage أدرج Folder باسم InventoryRemotes. وداخله أدرج ثلاثة RemoteEvent بالأسماء InventoryUpdated وEquipItem وUseItem.
  2. في ServerStorage أدرج Folder باسم ItemTools. وداخله أدرج Tool باسم Sword، وفي الـ Tool أدرج Part باسم Handle (مقاس 1 في 4 في 1 stud مناسب تمامًا). تأكد من إلغاء تحديد Anchored في Handle: تحذّر Roblox من أن الشخصية تعلق في مكانها إذا حملت أداة فيها أجزاء مثبّتة. ويصلح أيضًا Tool لديك بالفعل، إذا طابق اسمه الكتالوج في الخطوة 2.
  3. أضف السكربتات كلما تقدمت في الخطوات. والشجرة توضح أين يذهب كل منها.
Explorer بعد إنهاء الدليل
ReplicatedStorage
  ItemCatalog ModuleScript new
  InventoryRemotes Folder new
    InventoryUpdated RemoteEvent new
    EquipItem RemoteEvent new
    UseItem RemoteEvent new
ServerScriptService
  InventoryService ModuleScript new
  InventoryData Script new
  InventoryActions Script new
ServerStorage
  ItemTools Folder new
    Sword Tool new
      Handle Part new
StarterPlayer
  StarterPlayerScripts
    InventoryClient LocalScript new
Workspace
  Apple Part new
    ItemPickup Script new

الخطوة 2: صِف العناصر في الكتالوج

الكتالوج يصف كل عنصر يمكن أن يوجد، مرة واحدة. والمخزونات بعد ذلك تحفظ فقط الـ id الخاص بالعنصر وكميته. هكذا يبقى الحفظ صغيرًا، ويمكنك لاحقًا إعادة تسمية عنصر أو إعادة كتابة وصفه دون المساس ببيانات أحد المحفوظة.

أدرج ModuleScript في ReplicatedStorage، وسمّه ItemCatalog واستبدل محتواه بهذا:

ModuleScript في ReplicatedStorage باسم ItemCatalog
-- كل العناصر التي يمكن أن توجد في اللعبة. يقرأ هذه القائمة الخادم وكل
-- العملاء؛ والخادم وحده يقرر من يملك ماذا.
export type Item = {
	id: string, -- يجب أن يطابق المفتاح أدناه
	name: string,
	description: string,
	maxStack: number, -- كم من هذا العنصر يتسع في خانة واحدة
	icon: string?, -- صورة اختيارية: "rbxassetid://" مع ID صورتك
	toolName: string?, -- Tool في ServerStorage.ItemTools: يمكن تجهيز العنصر
	heal: number?, -- كم صحة يستعيد: العنصر مستهلَك
}

local items: { [string]: Item } = {
	sword = {
		id = "sword",
		name = "Sword",
		description = "Equip it to put it in your hand.",
		maxStack = 1,
		toolName = "Sword",
	},
	apple = {
		id = "apple",
		name = "Apple",
		description = "Use it to restore 25 health.",
		maxStack = 10,
		heal = 25,
	},
	wood = {
		id = "wood",
		name = "Wood",
		description = "A building material.",
		maxStack = 50,
	},
}

local ItemCatalog = {}

-- يُرجع العنصر صاحب هذا الـ id، أو nil إذا لم يوجد عنصر كهذا
function ItemCatalog.get(id: string): Item?
	return items[id]
end

return ItemCatalog

الحقول الاختيارية تقرر ماذا يفعل العنصر. toolName يجعله قابلًا للتجهيز، وheal يجعله قابلًا للاستخدام، والعنصر الذي ليس فيه أي منهما، مثل الخشب، يبقى في المخزون حتى يزيله كود آخر على الخادم (الصناعة، البيع).

لتضيف عنصرًا، أضف سجلًا يتطابق فيه المفتاح والـ id. المعرّفات حساسة لحالة الأحرف وهي ما يُحفظ، لذا لا تغيّر الـ id أبدًا بعد أن يمتلك اللاعبون العنصر: غيّر الـ name الخاص به.

الخطوة 3: احفظ المخزونات على الخادم

هذا الـ ModuleScript يملك مخزون كل لاعب. كل مخزون قائمة خانات، في كل خانة id عنصر وكمية، مع سعة 20 خانة. سكربتات الخادم الأخرى تستدعي دواله، ولا أحد غيره يعدّل الجدول.

أدرج ModuleScript في ServerScriptService وسمّه InventoryService:

ModuleScript في ServerScriptService باسم InventoryService
-- الكود الوحيد الذي يغيّر المخزونات. يوجد في ServerScriptService،
-- حيث لا يستطيع العملاء رؤيته ولا تشغيله.
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- وضع Strict لا يرى ما يُرجعه require عبر WaitForChild، لذا
-- نصف الجزء من الوحدة الذي يستخدمه هذا السكربت
type Item = { id: string, maxStack: number }
type Catalog = { get: (id: string) -> Item? }
local ItemCatalog = require(ReplicatedStorage:WaitForChild("ItemCatalog")) :: Catalog

local remotes = ReplicatedStorage:WaitForChild("InventoryRemotes")
local inventoryUpdated = remotes:WaitForChild("InventoryUpdated") :: RemoteEvent

export type Slot = { id: string, count: number }
type Inventory = { slots: { Slot }, capacity: number, equipped: string? }

local CAPACITY = 20 -- خانة لكل لاعب
local STARTER_ITEMS: { Slot } = { -- ما يبدأ به اللاعب الجديد كليًا
	{ id = "sword", count = 1 },
	{ id = "apple", count = 3 },
}

local inventories: { [Player]: Inventory } = {}

local InventoryService = {}

-- يرسل إلى المالك نسخة من مخزونه. يُستدعى بعد كل تغيير.
local function sync(player: Player)
	local inventory = inventories[player]
	if inventory then
		inventoryUpdated:FireClient(player, {
			capacity = inventory.capacity,
			slots = inventory.slots,
			equipped = inventory.equipped,
		})
	end
end

-- قواعد التكديس: نملأ الأكوام غير المكتملة أولًا، ثم نبدأ أكوامًا جديدة
-- في الخانات الفارغة. يُرجع الكمية المضافة، وقد تكون أقل من المطلوب.
local function addToInventory(inventory: Inventory, item: Item, amount: number): number
	local remaining = amount
	for _, slot in inventory.slots do
		if remaining == 0 then
			break
		end
		if slot.id == item.id and slot.count < item.maxStack then
			local moved = math.min(item.maxStack - slot.count, remaining)
			slot.count += moved
			remaining -= moved
		end
	end
	while remaining > 0 and #inventory.slots < inventory.capacity do
		local moved = math.min(item.maxStack, remaining)
		table.insert(inventory.slots, { id = item.id, count = moved })
		remaining -= moved
	end
	return amount - remaining
end

function InventoryService.count(player: Player, itemId: string): number
	local inventory = inventories[player]
	local total = 0
	if inventory then
		for _, slot in inventory.slots do
			if slot.id == itemId then
				total += slot.count
			end
		end
	end
	return total
end

-- كم يتسع بعد من العنصر: المكان في أكوامه مع الخانات الفارغة
function InventoryService.spaceFor(player: Player, itemId: string): number
	local inventory = inventories[player]
	local item = ItemCatalog.get(itemId)
	if not inventory or not item then
		return 0
	end
	local space = (inventory.capacity - #inventory.slots) * item.maxStack
	for _, slot in inventory.slots do
		if slot.id == itemId then
			space += item.maxStack - slot.count
		end
	end
	return space
end

-- يضيف حتى amount من العنصر. يُرجع الكمية المضافة (0 إذا
-- لم يوجد العنصر، أو امتلأ المخزون، أو لم يُحمَّل بعد).
function InventoryService.add(player: Player, itemId: string, amount: number): number
	local inventory = inventories[player]
	local item = ItemCatalog.get(itemId)
	local wanted = math.floor(amount)
	if not inventory or not item or wanted < 1 then
		return 0
	end
	local added = addToInventory(inventory, item, wanted)
	if added > 0 then
		sync(player)
	end
	return added
end

-- يزيل amount من العنصر بالضبط، بدءًا من الأكوام الأخيرة.
-- إذا كان لدى اللاعب أقل، لا يزيل شيئًا ويُرجع false.
function InventoryService.remove(player: Player, itemId: string, amount: number): boolean
	local inventory = inventories[player]
	local wanted = math.floor(amount)
	if not inventory or wanted < 1 or InventoryService.count(player, itemId) < wanted then
		return false
	end
	local remaining = wanted
	-- نبدأ من النهاية حتى لا يتخطى table.remove أي خانة
	for index = #inventory.slots, 1, -1 do
		local slot = inventory.slots[index]
		if slot.id == itemId then
			local taken = math.min(slot.count, remaining)
			slot.count -= taken
			remaining -= taken
			if slot.count == 0 then
				table.remove(inventory.slots, index)
			end
			if remaining == 0 then
				break
			end
		end
	end
	sync(player)
	return true
end

function InventoryService.getEquipped(player: Player): string?
	local inventory = inventories[player]
	return if inventory then inventory.equipped else nil
end

function InventoryService.setEquipped(player: Player, itemId: string?)
	local inventory = inventories[player]
	if inventory then
		inventory.equipped = itemId
		sync(player)
	end
end

-- يُستدعى عندما تُحمَّل بيانات اللاعب المحفوظة. saved هو ما
-- أرجعه مخزن البيانات: nil للاعب الجديد. كل سجل يمر من جديد عبر
-- قواعد التكديس، لذا لا تكسر العناصر المحذوفة وأحجام الأكوام القديمة شيئًا.
function InventoryService.load(player: Player, saved: any)
	local inventory: Inventory = { slots = {}, capacity = CAPACITY }
	local entries: { Slot } = STARTER_ITEMS
	if type(saved) == "table" and type(saved.slots) == "table" then
		entries = {}
		for _, entry in saved.slots do
			if type(entry) == "table" and type(entry.id) == "string" and type(entry.count) == "number" then
				table.insert(entries, { id = entry.id, count = math.floor(entry.count) })
			end
		end
	end
	for _, entry in entries do
		local item = ItemCatalog.get(entry.id)
		if item and entry.count > 0 then
			addToInventory(inventory, item, entry.count)
		end
	end
	inventories[player] = inventory
	sync(player)
end

-- نسخة بسيطة للحفظ، أو nil إذا لم تُحمَّل بيانات اللاعب أبدًا
function InventoryService.serialize(player: Player): { version: number, slots: { Slot } }?
	local inventory = inventories[player]
	if not inventory then
		return nil
	end
	local slots: { Slot } = {}
	for index, slot in inventory.slots do
		slots[index] = { id = slot.id, count = slot.count }
	end
	return { version = 1, slots = slots }
end

function InventoryService.unload(player: Player)
	inventories[player] = nil
end

return InventoryService

أسطر type والتحويل :: بعد require لا يحتاج إليها إلا مدقق الأنواع في Luau، الذي لا يرى ما تُرجعه الوحدة التي وُجدت عبر WaitForChild. وأثناء اللعب لا تغيّر شيئًا. وبقية السكربتات في هذا الدليل تفعل الشيء نفسه.

كيف تعمل قواعد التكديس مع الكتالوج أعلاه، حيث يتكدس التفاح حتى 10:

  • إذا أضفت 15 تفاحة للاعب لديه كومة من 7، تمتلئ هذه الكومة حتى 10، ثم تبدأ كومة جديدة من 5.
  • عندما تنفد الخانات، يضيف add ما اتسع ويُرجع هذا العدد. استدعِ spaceFor أولًا عندما تحتاج إلى الكل أو لا شيء، كما يفعل الالتقاط في الخطوة 5.
  • يعمل remove بمبدأ الكل أو لا شيء. طلب 3 تفاحات من لاعب لديه 2 لا يزيل شيئًا ويُرجع false، لذا تحقق من النتيجة قبل أن تعطي شيئًا في المقابل.
  • لا فجوات في الخانات: عندما تفرغ خانة، تتحرك البقية للأعلى. هكذا يسهل إرسال القائمة وحفظها. وعن الخانات التي تحتفظ بمواضعها تجد الشرح في الأسئلة في النهاية.
ما الذي تستطيع سكربتات الخادم الأخرى استدعاءه
الدالةتُرجعلماذا تُستخدم
add(player, itemId, amount)الكمية المضافةالالتقاط، والمكافآت، والمشتريات
remove(player, itemId, amount)true إذا أُزيل كل شيءالاستخدام، والبيع، والصناعة
count(player, itemId)المجموع عبر كل الأكوامالتحقق مما يملكه اللاعب
spaceFor(player, itemId)كم يتسع بعدفحص قبل الإضافة بمبدأ الكل أو لا شيء
getEquipped، setEquippedالـ id الخاص بالعنصر المجهّز أو nilالتجهيز، في الخطوة 6
load، serialize، unloadنسخة جاهزة للحفظ (serialize)التحميل والحفظ، في الخطوة 4

الخطوة 4: حمّل كل مخزون واحفظه

المخزونات تعيش في ذاكرة الخادم، لذا تختفي عندما يخرج اللاعب. هذا السكربت يحمّل خانات اللاعب من مخزن البيانات عند دخوله، ويكتبها عائدة عندما يخرج، وعندما يتوقف الخادم، وكل ثلاث دقائق تحسبًا لانهيار الخادم. ولا يحفظ إلا معرّفات العناصر وكمياتها: فمخازن البيانات لا تستطيع حفظ Instance، مثل Tool.

أدرج Script في ServerScriptService وسمّه InventoryData:

Script في ServerScriptService باسم InventoryData
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ServerScriptService = game:GetService("ServerScriptService")

type InventoryApi = {
	load: (player: Player, saved: any) -> (),
	serialize: (player: Player) -> any,
	unload: (player: Player) -> (),
}
local InventoryService = require(ServerScriptService:WaitForChild("InventoryService")) :: InventoryApi

local STORE_NAME = "PlayerInventory_v1"
local LOAD_ATTEMPTS = 3
local AUTOSAVE_SECONDS = 180

-- لا يُحفظ إلا اللاعبون الذين حُمّلت بياناتهم. بعد التحميل الفاشل
-- لا يجوز الحفظ، وإلا فسيحل مخزون فارغ محل المخزون الحقيقي.
local canSave: { [Player]: boolean } = {}
local activeSaves = 0

local function keyFor(player: Player): string
	return "User_" .. player.UserId
end

-- طلب فاشل واحد يجب ألا يكلّف أحدًا عناصره، لذا نحاول عدة مرات.
-- GetDataStore أيضًا داخل pcall، لذا فجلسة Studio دون وصول إلى مخازن
-- البيانات تحصل على تحذير، ولا يتوقف هذا السكربت.
local function loadSaved(player: Player): (boolean, any)
	for attempt = 1, LOAD_ATTEMPTS do
		local ok, result = pcall(function()
			return DataStoreService:GetDataStore(STORE_NAME):GetAsync(keyFor(player))
		end)
		if ok then
			return true, result
		end
		warn(`Inventory load failed for {player.Name} (attempt {attempt}): {result}`)
		if attempt < LOAD_ATTEMPTS then
			task.wait(attempt * 2)
		end
	end
	return false, nil
end

local function onPlayerAdded(player: Player)
	local ok, saved = loadSaved(player)
	if player.Parent ~= Players then
		return -- خرج اللاعب أثناء التحميل
	end
	if ok then
		InventoryService.load(player, saved) -- saved يساوي nil للاعب الجديد
		canSave[player] = true
	elseif RunService:IsStudio() then
		-- يعني عادة أن وصول Studio إلى API services معطّل. نلعب بـ
		-- عناصر البداية حتى يمكن الاختبار، لكننا لا نحفظها أبدًا.
		warn("Inventory not loaded: playing with starter items that will not be saved")
		InventoryService.load(player, nil)
	else
		player:Kick("Your inventory could not be loaded. Please rejoin in a minute.")
	end
end

local function save(player: Player)
	if not canSave[player] then
		return -- لم تُحمَّل البيانات أبدًا، فلا شيء يمكن كتابته بأمان
	end
	local data = InventoryService.serialize(player)
	if not data then
		return
	end
	activeSaves += 1
	local ok, err = pcall(function()
		DataStoreService:GetDataStore(STORE_NAME):SetAsync(keyFor(player), data, { player.UserId })
	end)
	activeSaves -= 1
	if not ok then
		warn(`Inventory save failed for {player.Name}: {err}`)
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in Players:GetPlayers() do
	task.spawn(onPlayerAdded, player)
end

Players.PlayerRemoving:Connect(function(player: Player)
	save(player)
	canSave[player] = nil
	InventoryService.unload(player)
end)

-- عندما يتوقف الخادم، نحفظ كل من ما زال هنا، وننتظر كل
-- عملية حفظ جارية، ومنها ما بدأه PlayerRemoving
game:BindToClose(function()
	for _, player in Players:GetPlayers() do
		task.spawn(save, player)
	end
	while activeSaves > 0 do
		task.wait()
	end
end)

-- نحفظ الجميع كل بضع دقائق، حتى يكلّف انهيار الخادم دقائق لا الجلسة كلها
while true do
	task.wait(AUTOSAVE_SECONDS)
	for _, player in Players:GetPlayers() do
		task.spawn(save, player)
	end
end

ماذا يفعل السكربت ولماذا:

  • التحميل مع القيم الافتراضية. اللاعب الجديد ليس لديه قيمة محفوظة، لذا يُرجع GetAsync القيمة nil، فيحصل على عناصر البداية. والخانات المحفوظة تمر من جديد عبر قواعد التكديس، لذا لن يكسر التحميلَ عنصرٌ حُذف من الكتالوج أو حجم كومة أقصى صار أصغر. وكل ما لم يعد يتسع أو لم يعد الـ id الخاص به في الكتالوج يُهمل، والحفظ التالي يجعل ذلك نهائيًا، لذا أبقِ في الكتالوج كل id قد يملكه اللاعبون.
  • لا حفظ أبدًا بعد التحميل الفاشل. لا يُكتب إلا اللاعبون الموجودون في canSave. وعلى الخادم الحي يُطلب من اللاعب الذي لم تُحمَّل بياناته حتى بعد ثلاث محاولات أن يعيد الدخول. أما في Studio فيحصل بدلًا من ذلك على عناصر بداية لا تُحفظ، بعد بضع ثوانٍ من التأخير بسبب إعادة المحاولة. وسيبقى الأمر كذلك حتى يُنشر المكان مع تفعيل الوصول إلى الواجهة.
  • الحفظ عند الخروج وعند الإيقاف. يحفظ BindToClose كل من ما زال على الخادم وينتظر عمليات الحفظ الجارية. وتمنح Roblox الدوال المربوطة حتى 30 ثانية.
  • عدم تجاوز الحدود. كتابة واحدة لكل لاعب كل ثلاث دقائق مع واحدة عند الخروج أقل بكثير من الميزانية القياسية للخادم البالغة 60 كتابة في الدقيقة مع 40 لكل لاعب (بحسب التوثيق في 30 سبتمبر 2026). ونمط المفتاح User_ مأخوذ من توصيات Roblox، وتمرير user ID إلى SetAsync يربط البيانات بهذا اللاعب، وهو ما توصي به Roblox بشدة لمعالجة طلبات حذف البيانات.

للعبة صغيرة هذا أساس متين. أما قفل الجلسة، الذي يمنع لاعبًا غيّر الخادم بسرعة من تحميل البيانات قبل أن يحفظها الخادم القديم، ومتى تستخدم UpdateAsync بدلًا من SetAsync، فمشروحان في شرح DataStore.

الخطوة 5: أعطِ العناصر من العالم

الالتقاط أبسط طريقة تدخل بها العناصر المخزون: جزء فيه ProximityPrompt يضيف العنصر عندما يستخدمه اللاعب. يستمع السكربت إلى حدثه Triggered على الخادم، لذا يُضاف العنصر حيث لا يستطيع العميل التدخل.

  1. أضف Part إلى Workspace، وسمّه Apple ولوّنه بالأحمر. فعّل Anchored وعطّل CanCollide، حتى لا يصطدم به أحد وهو مخفي. والتلميح يعرض اسم الجزء.
  2. مع تحديد الجزء، مرّر نافذة Properties حتى قسم Attributes واضغط +. أضف ItemId بالنوع string واضغط Save، ثم اجعل القيمة apple. وبالطريقة نفسها أضف Amount بالنوع number وقيمته 1.
  3. أدرج Script في الجزء، وسمّه ItemPickup والصق هذا الكود:
Script في الجزء Apple في Workspace باسم ItemPickup
-- ضع هذا الـ Script داخل Part وأعطِ الـ Part سمتين:
-- ItemId (نص، مثل apple) وAmount (رقم، مثل 1).
local ServerScriptService = game:GetService("ServerScriptService")

type InventoryApi = {
	add: (player: Player, itemId: string, amount: number) -> number,
	spaceFor: (player: Player, itemId: string) -> number,
}
local InventoryService = require(ServerScriptService:WaitForChild("InventoryService")) :: InventoryApi

local part = script.Parent :: BasePart
local RESPAWN_SECONDS = 15

local itemId = part:GetAttribute("ItemId")
local amount = part:GetAttribute("Amount")
if typeof(itemId) ~= "string" or typeof(amount) ~= "number" or amount < 1 then
	error(`{part:GetFullName()} needs an ItemId (string) and an Amount (number, 1 or more) attribute`)
end

local prompt = Instance.new("ProximityPrompt")
prompt.ActionText = "Pick up"
prompt.ObjectText = part.Name
prompt.Parent = part

local visibleTransparency = part.Transparency

prompt.Triggered:Connect(function(player: Player)
	if not prompt.Enabled then
		return -- التقطه شخص آخر للتو
	end
	-- نتحقق من المسافة على الخادم، ولا نصدّق الطلب
	local character = player.Character
	if not character then
		return
	end
	local distance = (character:GetPivot().Position - part.Position).Magnitude
	if distance > prompt.MaxActivationDistance + 5 then
		return
	end
	if InventoryService.spaceFor(player, itemId) < amount then
		return -- المكان لا يكفي: نترك العنصر في مكانه
	end
	InventoryService.add(player, itemId, amount)

	prompt.Enabled = false
	part.Transparency = 1
	task.wait(RESPAWN_SECONDS)
	part.Transparency = visibleTransparency
	prompt.Enabled = true
end)

انسخ الجزء لتوزع عناصر أكثر. وغيّر السمات لتعطي خشبًا أو سيفًا آخر أو كومة تفاح.

تتحقق Roblox من المسافة لـ Triggered، لكن دليل الأمان لديها يأمر مع ذلك بالتحقق من التلميحات على الخادم: فالغشاش يستطيع استدعاء أحداث التلميح حتى عندما تكون Enabled على false، ويستطيع جذب جزء غير مثبّت إلى شخصيته. لهذا يتحقق السكربت بنفسه من Enabled ومن المسافة، ولهذا الجزء مثبّت.

أي سكربت آخر على الخادم يعطي العناصر بالطريقة نفسها. بعد شراء أو مهمة أو قتل، استدعِ InventoryService عبر require كما تفعل السكربتات أعلاه، ثم استدعِ InventoryService.add(player, "wood", 5). وتحقق من spaceFor أولًا عندما لا يجوز أن يضيع العنصر. ويمكن أن يكون الـ NPC مصدرًا آخر: في دليل حوارات NPC يُطلق الرد إجراءً على الخادم، ويمكن لهذا الإجراء أن يستدعي InventoryService.add بالطريقة نفسها.

الخطوة 6: تجهيز الـ Tool واستخدام العناصر

يحمل حدثا RemoteEvent الطلبين الوحيدين اللذين يستطيع العميل تقديمهما. EquipItem يضع نسخة من الـ Tool الخاص بالعنصر من ServerStorage في Backpack اللاعب (شريط الأدوات أسفل الشاشة) وفي يده، والطلب الثاني يزيلها. وUseItem يزيل قطعة واحدة من العنصر القابل للاستخدام ويطبّق تأثيره، وهو هنا العلاج.

أدرج Script في ServerScriptService وسمّه InventoryActions:

Script في ServerScriptService باسم InventoryActions
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")

type Item = { id: string, toolName: string?, heal: number? }
type Catalog = { get: (id: string) -> Item? }
type InventoryApi = {
	count: (player: Player, itemId: string) -> number,
	remove: (player: Player, itemId: string, amount: number) -> boolean,
	getEquipped: (player: Player) -> string?,
	setEquipped: (player: Player, itemId: string?) -> (),
}
local ItemCatalog = require(ReplicatedStorage:WaitForChild("ItemCatalog")) :: Catalog
local InventoryService = require(ServerScriptService:WaitForChild("InventoryService")) :: InventoryApi

local remotes = ReplicatedStorage:WaitForChild("InventoryRemotes")
local equipItem = remotes:WaitForChild("EquipItem") :: RemoteEvent
local useItem = remotes:WaitForChild("UseItem") :: RemoteEvent
local itemTools = ServerStorage:WaitForChild("ItemTools")

local COOLDOWN_SECONDS = 0.25
local lastRequest: { [Player]: number } = {}

-- كل طلب يبدأ هنا. يستطيع العميل إرسال أي شيء في أي وقت، لذا
-- نتحقق من النوع، ومن وجود العنصر، ومن أن اللاعب يملكه، ومن المعدّل.
local function ownedItem(player: Player, itemId: unknown): Item?
	if typeof(itemId) ~= "string" then
		return nil
	end
	local now = os.clock()
	if now - (lastRequest[player] or 0) < COOLDOWN_SECONDS then
		return nil
	end
	lastRequest[player] = now
	local item = ItemCatalog.get(itemId)
	if not item or InventoryService.count(player, itemId) < 1 then
		return nil
	end
	return item
end

-- يدمّر الـ Tool الخاص بهذا العنصر أينما كانت النسخة: شريط الأدوات أو اليد أو StarterGear
local function destroyTools(container: Instance?, itemId: string)
	if not container then
		return
	end
	for _, child in container:GetChildren() do
		if child:IsA("Tool") and child:GetAttribute("InventoryItem") == itemId then
			child:Destroy()
		end
	end
end

local function unequip(player: Player)
	local equippedId = InventoryService.getEquipped(player)
	if equippedId then
		destroyTools(player:FindFirstChildOfClass("Backpack"), equippedId)
		destroyTools(player.Character, equippedId)
		destroyTools(player:FindFirstChildOfClass("StarterGear"), equippedId)
		InventoryService.setEquipped(player, nil)
	end
end

local function equip(player: Player, item: Item, toolName: string)
	local template = itemTools:FindFirstChild(toolName)
	local backpack = player:FindFirstChildOfClass("Backpack")
	if not template or not template:IsA("Tool") or not backpack then
		warn(`Cannot equip {item.id}: no Tool named {toolName} in ServerStorage.ItemTools`)
		return
	end
	unequip(player) -- أداة مجهّزة واحدة في كل مرة

	local tool = template:Clone()
	tool.CanBeDropped = false -- النسخة المُسقطة قد يلتقطها شخص آخر
	tool:SetAttribute("InventoryItem", item.id)
	tool.Parent = backpack

	-- النسخة الثانية في StarterGear تُنسخ عائدة إلى Backpack بعد الـ respawn
	local starterGear = player:FindFirstChildOfClass("StarterGear")
	if starterGear then
		local gearCopy = tool:Clone()
		gearCopy.Parent = starterGear
	end

	local character = player.Character
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")
	if humanoid and humanoid.Health > 0 then
		humanoid:EquipTool(tool) -- إلى اليد مباشرة
	end
	InventoryService.setEquipped(player, item.id)
end

equipItem.OnServerEvent:Connect(function(player: Player, itemId: unknown)
	local item = ownedItem(player, itemId)
	if not item or not item.toolName then
		return
	end
	if InventoryService.getEquipped(player) == item.id then
		unequip(player) -- النقر مجددًا على العنصر المجهّز يزيله
	else
		equip(player, item, item.toolName)
	end
end)

useItem.OnServerEvent:Connect(function(player: Player, itemId: unknown)
	local item = ownedItem(player, itemId)
	if not item or not item.heal then
		return
	end
	local heal = item.heal
	local character = player.Character
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid.Health <= 0 or humanoid.Health >= humanoid.MaxHealth then
		return -- ميت أو صحته كاملة بالفعل: لا نستهلك العنصر
	end
	-- نأخذ العنصر أولًا، ونعالج فقط إذا نجح ذلك
	if InventoryService.remove(player, item.id, 1) then
		humanoid.Health = math.min(humanoid.MaxHealth, humanoid.Health + heal)
	end
end)

Players.PlayerRemoving:Connect(function(player: Player)
	lastRequest[player] = nil
end)
ما المسموح لكل طلب
الطلبيرسل العميليتحقق الخادم قبل التنفيذ
EquipItemالـ id الخاص بالعنصرأنه نص، وأن العنصر موجود، وأن اللاعب يملكه، وأن الطلبات لا تتجاوز أربعة في الثانية، وأن للعنصر toolName، وأن Tool بهذا الاسم موجود في ServerStorage.ItemTools
UseItemالـ id الخاص بالعنصرفحوص النوع والملكية والمعدّل نفسها، وأن للعنصر قيمة heal، وأن الشخصية حية ومصابة، وأن عنصرًا واحدًا أُزيل قبل إضافة الصحة

ثلاث تفاصيل تمنع الـ Tool من التكرار أو الضياع. نسخة الأداة فيها CanBeDropped على false، لأن الـ Tool المُسقط يقع في Workspace حيث يستطيع لاعب آخر أخذه، بينما يبقى الأصل في المخزون. وكل نسخة تحمل السمة InventoryItem، لذا يمكن عند نزع التجهيز إيجاد كل نسخة وتدميرها أينما كانت. وإذا كان لدى اللاعب حاوية StarterGear فتوضع فيها نسخة ثانية. ينسخ Roblox الـ StarterGear إلى Backpack كلما ظهرت الشخصية، لذا يعود السيف المجهّز بعد الـ respawn.

لا يُجهَّز إلا Tool واحد في كل مرة، والتجهيز لا يُحفظ، لذا يبدأ اللاعب بعد إعادة الدخول بشريط أدوات فارغ. وإذا أضفت لاحقًا البيع أو الإسقاط، فانزع تجهيز العنصر قبل أن تغادر آخر نسخة منه المخزون.

يمكنك تحويل السيف المؤقت إلى سيف يقاتل فعلًا: ابنِ الـ Tool وفق دليل السيف، وأبقِ اسمه Sword وضعه في ItemTools بدلًا من المؤقت. وسيتحقق سكربت الخادم من ذلك الدليل من كل ضربة، أيًا كانت الطريقة التي حصل بها اللاعب على السيف.

الخطوة 7: ارسم شبكة المخزون

يبني الـ LocalScript النافذة كلها بالكود، لذا لا حاجة لترتيب أي شيء يدويًا. يحمل ScrollingFrame الخانات، ويرتبها UIGridLayout داخله في صفوف، ويكبّر AutomaticCanvasSize منطقة التمرير كلما أُضيفت صفوف.

في Explorer افتح StarterPlayer، وأدرج LocalScript في StarterPlayerScripts وسمّه InventoryClient:

LocalScript في StarterPlayerScripts باسم InventoryClient
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

type Item = { name: string, description: string, icon: string?, toolName: string?, heal: number? }
type Catalog = { get: (id: string) -> Item? }
type Slot = { id: string, count: number }
type Snapshot = { capacity: number, slots: { Slot }, equipped: string? }

local ItemCatalog = require(ReplicatedStorage:WaitForChild("ItemCatalog")) :: Catalog
local remotes = ReplicatedStorage:WaitForChild("InventoryRemotes")
local inventoryUpdated = remotes:WaitForChild("InventoryUpdated") :: RemoteEvent
local equipItem = remotes:WaitForChild("EquipItem") :: RemoteEvent
local useItem = remotes:WaitForChild("UseItem") :: RemoteEvent

local player = Players.LocalPlayer
local TOGGLE_KEY = Enum.KeyCode.B

local COLOURS = {
	panel = Color3.fromRGB(24, 20, 36),
	slot = Color3.fromRGB(44, 38, 64),
	empty = Color3.fromRGB(34, 30, 48),
	equipped = Color3.fromRGB(124, 77, 255),
	text = Color3.fromRGB(240, 238, 245),
	muted = Color3.fromRGB(170, 165, 185),
}

local function addCorner(parent: GuiObject, radius: number)
	local corner = Instance.new("UICorner")
	corner.CornerRadius = UDim.new(0, radius)
	corner.Parent = parent
end

local function makeLabel(parent: GuiObject, text: string, textSize: number): TextLabel
	local label = Instance.new("TextLabel")
	label.BackgroundTransparency = 1
	label.Font = Enum.Font.BuilderSansBold
	label.TextColor3 = COLOURS.text
	label.TextSize = textSize
	label.Text = text
	label.Parent = parent
	return label
end

-- 1. نبني النافذة مرة واحدة. ResetOnSpawn = false يُبقيها بعد الـ respawn.
local gui = Instance.new("ScreenGui")
gui.Name = "InventoryGui"
gui.ResetOnSpawn = false
gui.Parent = player:WaitForChild("PlayerGui")

local openButton = Instance.new("TextButton")
openButton.AnchorPoint = Vector2.new(0, 0.5)
openButton.Position = UDim2.new(0, 16, 0.5, 0)
openButton.Size = UDim2.fromOffset(140, 40)
openButton.BackgroundColor3 = COLOURS.panel
openButton.Font = Enum.Font.BuilderSansBold
openButton.TextColor3 = COLOURS.text
openButton.TextSize = 18
openButton.Text = "Inventory (B)"
addCorner(openButton, 8)
openButton.Parent = gui

local panel = Instance.new("Frame")
panel.AnchorPoint = Vector2.new(0.5, 0.5)
panel.Position = UDim2.fromScale(0.5, 0.5)
panel.Size = UDim2.fromScale(0.9, 0.8)
panel.BackgroundColor3 = COLOURS.panel
panel.Visible = false
addCorner(panel, 12)
panel.Parent = gui

local sizeLimit = Instance.new("UISizeConstraint")
sizeLimit.MaxSize = Vector2.new(440, 400)
sizeLimit.Parent = panel

local title = makeLabel(panel, "Inventory", 22)
title.Position = UDim2.fromOffset(16, 8)
title.Size = UDim2.new(1, -32, 0, 32)
title.TextXAlignment = Enum.TextXAlignment.Left

local slotCount = makeLabel(panel, "", 16)
slotCount.Position = UDim2.fromOffset(16, 8)
slotCount.Size = UDim2.new(1, -32, 0, 32)
slotCount.TextColor3 = COLOURS.muted
slotCount.TextXAlignment = Enum.TextXAlignment.Right

local grid = Instance.new("ScrollingFrame")
grid.Position = UDim2.fromOffset(12, 48)
grid.Size = UDim2.new(1, -24, 1, -100)
grid.BackgroundTransparency = 1
grid.BorderSizePixel = 0
grid.CanvasSize = UDim2.new() -- AutomaticCanvasSize يكبّره ليناسب الخلايا
grid.AutomaticCanvasSize = Enum.AutomaticSize.Y
grid.ScrollingDirection = Enum.ScrollingDirection.Y
grid.ScrollBarThickness = 6
grid.Parent = panel

local layout = Instance.new("UIGridLayout")
layout.CellSize = UDim2.fromOffset(72, 72)
layout.CellPadding = UDim2.fromOffset(8, 8)
layout.SortOrder = Enum.SortOrder.LayoutOrder
layout.Parent = grid

local info = makeLabel(panel, "Click an item to equip or use it.", 16)
info.AnchorPoint = Vector2.new(0, 1)
info.Position = UDim2.new(0, 16, 1, -8)
info.Size = UDim2.new(1, -32, 0, 40)
info.Font = Enum.Font.BuilderSans
info.TextColor3 = COLOURS.muted
info.TextWrapped = true
info.TextXAlignment = Enum.TextXAlignment.Left

-- 2. خلية واحدة لكل خانة. هذا يرسم فقط ما أرسله الخادم؛ والمخزون
-- نفسه لا يتغير من هنا أبدًا.
local function makeCell(index: number, slot: Slot?, equippedId: string?)
	local cell = Instance.new("TextButton")
	cell.LayoutOrder = index
	cell.Text = ""
	cell.AutoButtonColor = slot ~= nil
	cell.BackgroundColor3 = if slot then COLOURS.slot else COLOURS.empty
	addCorner(cell, 8)
	cell.Parent = grid
	if not slot then
		return -- خانة فارغة
	end

	local itemId = slot.id
	local item = ItemCatalog.get(itemId)
	local name = if item then item.name else itemId

	if item and item.icon then
		local image = Instance.new("ImageLabel")
		image.BackgroundTransparency = 1
		image.Size = UDim2.fromScale(1, 1)
		image.Image = item.icon
		image.Parent = cell
	else
		local nameLabel = makeLabel(cell, name, 14)
		nameLabel.Size = UDim2.fromScale(1, 1)
		nameLabel.TextWrapped = true
	end

	if slot.count > 1 then
		local countLabel = makeLabel(cell, `x{slot.count}`, 14)
		countLabel.AnchorPoint = Vector2.new(1, 1)
		countLabel.Position = UDim2.new(1, -6, 1, -4)
		countLabel.Size = UDim2.fromOffset(40, 16)
		countLabel.TextXAlignment = Enum.TextXAlignment.Right
	end

	if itemId == equippedId then
		local outline = Instance.new("UIStroke")
		outline.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
		outline.Color = COLOURS.equipped
		outline.Thickness = 3
		outline.Parent = cell
	end

	cell.MouseEnter:Connect(function()
		info.Text = if item then `{item.name}: {item.description}` else name
	end)
	cell.Activated:Connect(function()
		-- نطلب ولا نأمر: الخادم يتحقق من الطلب ويرسل النتيجة
		if item and item.toolName then
			equipItem:FireServer(itemId)
		elseif item and item.heal then
			useItem:FireServer(itemId)
		end
	end)
end

local function render(snapshot: Snapshot)
	for _, child in grid:GetChildren() do
		if child:IsA("GuiButton") then
			child:Destroy()
		end
	end
	for index = 1, snapshot.capacity do
		makeCell(index, snapshot.slots[index], snapshot.equipped)
	end
	slotCount.Text = `{#snapshot.slots} / {snapshot.capacity} slots`
end

-- يرسل الخادم هذا بعد التحميل وبعد كل تغيير
inventoryUpdated.OnClientEvent:Connect(render)

-- 3. نفتح ونغلق بالزر أو بالمفتاح B
local function toggle()
	panel.Visible = not panel.Visible
end

openButton.Activated:Connect(toggle)
UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if not gameProcessedEvent and input.KeyCode == TOGGLE_KEY then
		toggle()
	end
end)

الـ LocalScript في StarterPlayerScripts يعمل مرة واحدة في كل جلسة، وResetOnSpawn = false يُبقي الـ ScreenGui عند الـ respawn، لذا تستمر الشبكة في عرض آخر نسخة أرسلها الخادم.

يرسم العميل خلية لكل خانة، بما فيها الفارغة، لذا يبدو مخزون الـ 20 خانة دائمًا 20 خانة. كل تحديث يدمّر الخلايا القديمة ويبني جديدة، وهذا بسيط ويناسب بضع عشرات من الخانات. وإذا أرسل الخادم InventoryUpdated قبل أن يتصل به هذا السكربت، فستضع Roblox الرسالة في قائمة انتظار وتسلّمها حالما يتصل المعالج، لذا لن يضيع المخزون الأول.

تريد رسم النافذة في Studio؟ ابنِ ScreenGui مع خانة قالب واحدة واستدعِ عليها Clone داخل makeCell. وللأيقونات، أعطِ العنصر في الكتالوج icon بصورة رفعتها، وستعرض خليته الصورة بدل الاسم.

اختبر المخزون

  1. اضغط Play. افتح المخزون بالزر على اليسار أو بالمفتاح B: لدى اللاعب الجديد Sword و3 Apple، والعدّاد يُظهر 2 / 20 slots. وما دام Studio لا يستطيع الوصول إلى مخازن البيانات، تظهر العناصر بعد بضع ثوانٍ، بعد تحذيرات التحميل في Output، ولا تُحفظ.
  2. انقر على Sword. سيظهر السيف في يدك وفي شريط الأدوات، وستحصل خانته على إطار بنفسجي. انقر مرة أخرى لتزيله.
  3. اقترب من الجزء Apple واستخدم تلميحه. ستكبر كومة التفاح إلى 4، وسيختفي الجزء 15 ثانية.
  4. لتختبر العلاج، خفّض الصحة أولًا. بدّل Client/Server إلى Server، وحدد Humanoid شخصيتك في Explorer، واجعل Health تساوي 50، ثم عُد إلى Client وانقر على Apple.
  5. مع تفعيل وصول Studio إلى API services، أوقف اللعبة وشغّلها مجددًا. التفاحة التي التقطتها ما زالت موجودة. وإذا فشل التحميل أو الحفظ فسيخبرك Output بذلك.

لتختبر مع عدة لاعبين، اختر في إعدادات الاختبار Server & Clients مع عميلين اثنين (2). كل لاعب يرى مخزونه فقط، والسيف في يد الآخر يراه الاثنان.

الأخطاء الشائعة وحلولها

معظم المشكلات تظهر في نافذة Output تحذيرًا أو سطر خطأ أحمر. اقرأ الأول: فالأخطاء التالية كثيرًا ما تنتج عنه.

Infinite yield possible عند استدعاء WaitForChild

السكربت ينتظر كائنًا غير موجود بهذا الاسم بالضبط. قارن Explorer عندك بالشجرة في الخطوة 1: InventoryRemotes مع ثلاثة RemoteEvent في ReplicatedStorage، وItemTools في ServerStorage، والوحدتان. الأسماء حساسة لحالة الأحرف. ودليل أخطاء السكربتات يشرح هذا التحذير بتفصيل أكبر.

الشبكة تُفتح لكنها تبقى فارغة

العميل لا يرسم إلا ما يرسله الخادم. ابحث في Output عن تحذيرات Inventory load failed، وتأكد من أن InventoryData هو Script لا LocalScript، وأنه في ServerScriptService وبجانبه الـ ModuleScript InventoryService. وإذا أظهر Output خطأ Remote event invocation، فلا أحد على العميل يستمع إلى InventoryUpdated: ابحث عن خطأ أقدم من InventoryClient.

العناصر تُصفَّر في كل مرة ألعب فيها في Studio

إذا كتب Output عبارة Inventory not loaded: playing with starter items that will not be saved، فالمكان غير منشور أو وصول Studio إلى API services معطّل. فعّله كما في الخطوة 4، في نسخة اختبار من لعبتك.

النقر على السيف لا يفعل شيئًا

ابحث في Output عن Cannot equip sword: يجب أن يكون اسم الـ Tool في ServerStorage.ItemTools مطابقًا تمامًا لـ toolName في الكتالوج. ويحتاج الـ Tool أيضًا إلى جزء باسم Handle حتى يمكن حمله، ما لم تجعل RequiresHandle فيه false.

تلميح الالتقاط يظهر، لكن لا شيء يُضاف

يجب أن تطابق السمة ItemId الـ id في الكتالوج تمامًا، بما في ذلك حالة الأحرف: apple لا Apple. ولا يُضاف شيء ما دام مخزون اللاعب يُحمَّل، أو عندما لا يوجد مكان للكمية كلها.

الحفظ ينكسر بعد أن غيّرت ما يُحفظ

مخازن البيانات تقبل بيانات بسيطة: النصوص والأرقام والقيم المنطقية والجداول المكوّنة منها. حفظ Tool أو Part أو أي Instance آخر يفشل، أو تُحفظ القيمة nil. احفظ الـ id الخاص بالعنصر وكميته، وأعد بناء كل ما عدا ذلك من الكتالوج، كما يفعل serialize.

اصنعه مع RoCode

إذا كان وصف النظام أسهل عليك من بنائه يدويًا، يستطيع RoCode بناءه في المكان المفتوح عندك. إنه وكيل ذكاء اصطناعي لـ Roblox Studio: تصف الميزة في دردشة الويب، وتنشئ إضافته لـ Studio السكربتات والكائنات في Explorer عندك، بدلًا من إعطائك كودًا لتلصقه.

ما تكتبه

أضف مخزونًا من 20 خانة مع أكوام للعناصر، وشبكة واجهة تُفتح بالمفتاح B، وسيفًا من ServerStorage يستطيع اللاعبون تجهيزه، وتفاحًا يعالج، وحفظًا عبر DataStoreService.

ما يفعله RoCode
  • يمكنه تصفح مكانك عندما تحتاج الخطوة إلى ذلك، عبر Search Game وSearch Scripts، ليبني على العناصر أو الـ remotes أو كود الحفظ الموجود لديك بدلًا من إضافة نظام ثانٍ
  • ينشئ المجلدات والـ RemoteEvent عبر Create Instance، والـ ModuleScript والـ Script والـ LocalScript عبر Create Script
  • يمكنه بناء هيكل النافذة عبر Build UI، وفيه تخطيط مخزون بخانات مؤقتة؛ أما الكود الذي يملأ الخانات من الخادم فيُكتب في سكربتات منفصلة
  • يفحص ترجمة كل سكربت في Studio عبر Check Script، الذي يلتقط أخطاء الترجمة وبعض الأنماط الخطرة، لكنه لا يفحص الأنواع ولا يدقق الأسلوب ولا يشغّل كودك

كل دفعة تغييرات نقطة تراجع في Studio. لا يستطيع RoCode تفعيل وصول Studio إلى API services، فهذا تفعله بنفسك. ومع الإضافة 3.9 أو أحدث يستطيع تشغيل اختبار لعب قصير وقراءة الأخطاء، ومع الإضافة 3.9.15 أو أحدث تستطيع شخصية الاختبار لديه الضغط على المفاتيح وتجهيز العناصر، لكنه لا يفحص إلا ما قرر هو اختباره، لذا اختبر الحفظ بنفسك في اختبار لعب. المخزون يقوم فوق أنظمة أخرى، مثل الحفظ والأدوات، وصانع الألعاب بالذكاء الاصطناعي الذي يبني في مكانك أنت في Studio يضيفها طلبًا تلو الآخر. كيف يتصل RoCode بـ Studio.

الأسئلة الشائعة

هل أستخدم Backpack في Roblox بدلًا من مخزوني الخاص؟

الـ Backpack هو المخزون المدمج في Roblox: كل Tool فيه يظهر زرًا في شريط الأدوات. إذا كانت كل العناصر في لعبتك من نوع Tool ولا تحتاج إلى أكوام وكميات وحد للخانات، فقد يكفيك، مع أنك ستظل مضطرًا لحفظ أي Tools يملكها كل لاعب. أما المخزون الخاص فتحتاج إليه للعناصر التي ليست Tool، مثل المواد والمستهلكات. وفي هذا الدليل نستخدم الاثنين: المخزون يحفظ كل شيء، والـ Backpack يحمل الـ Tool المجهّز.

كيف أخفي شريط الأدوات الافتراضي في Roblox؟

من LocalScript استدعِ StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false). وعندما يكون شريط الأدوات مخفيًا، يحتاج اللاعبون إلى طريقة أخرى لأخذ الـ Tool المجهّز وإزالته، مثل زر يطلب من الخادم استدعاء Humanoid:EquipTool أو Humanoid:UnequipTools.

كيف أجعل الخانات تحتفظ بمواضعها للسحب والإفلات (drag and drop)؟

احفظ قائمة بطول ثابت فيها سجل واحد لكل خانة، وعلّم الخانات الفارغة بقيمة مثل id فارغ لا بـ nil، لأن Roblox لا تنصح بإرسال جداول فيها nil عبر RemoteEvent. ثم أضف RemoteEvent باسم MoveItem يرسل رقمَي خانتين، واجعل الخادم يتحقق من أن كليهما عدد صحيح من 1 إلى السعة قبل أن يبدّل بينهما.

ما الحجم الذي يمكن أن يبلغه المخزون المحفوظ؟

قيمة واحدة في مخزن البيانات يمكن أن تشغل حتى 4,194,304 حرفًا بعد التسلسل (بحسب التوثيق في 30 سبتمبر 2026). وقائمة المعرّفات والكميات تشغل جزءًا ضئيلًا من ذلك. ما يكبّر حجم الحفظ هو النص لكل عنصر، مثل الأسماء المخصصة أو الأوصاف الطويلة المحفوظة لكل عنصر.

كيف أشتري العناصر وأبيعها بالعملات؟

افعل كل شيء على الخادم. لتبيع، استدعِ InventoryService.remove وادفع فقط إذا أرجع true. ولتشتري، تحقق من السعر ومن spaceFor، وخذ العملات، ثم استدعِ add. ودليل المتجر يبني جانب الشراء.

المصادر والقراءات

كيف فُحص الكود: كل سكربت في هذه الصفحة يجتاز الفاحص الصارم للأنواع في Luau مع تعريفات واجهة Roblox البرمجية (luau-lsp، ٢٠ نوفمبر ٢٠٢٦). فحص الأنواع يكشف الأخطاء في أسماء الواجهات البرمجية والأنواع الخاطئة، لا منطق اللعبة، فجرّب الكود في Studio قبل النشر.

جرّب RoCode على لعبتك أنت.

رصيد مجاني كل يوم ودون بطاقة. ثبّت الإضافة، وصف الميزة، وراجع ما يظهر في Studio.