בקצרה
שמרו את הפריטים של כל שחקן בטבלה בשרת, בתוך ModuleScript ב-ServerScriptService, ורק הקוד הזה יכול להוסיף, להסיר ולערום אותם. תארו את הפריטים פעם אחת ב-ModuleScript ב-ReplicatedStorage (id, שם, ערימה מקסימלית). אחרי כל שינוי השרת שולח לשחקן הזה RemoteEvent עם עותק של המלאי שלו, ו-LocalScript מצייר אותו כרשת דרך ScrollingFrame ו-UIGridLayout.
- הלקוח אף פעם לא מוסיף פריטים. הוא יכול רק לבקש, למשל "תצייד אותי בחרב שלי", והשרת בודק שהיא באמת אצל השחקן לפני שהוא עושה משהו.
- פריטים מופיעים רק דרך קוד שרת שאתם שולטים בו: איסוף, קניות בחנות אחרי בדיקה בשרת, פרסים.
- שמרו את ה-id של הפריטים ואת הכמות שלהם, לא Tool, דרך DataStoreService, כשהשחקן יוצא, כשהשרת נכבה וכל כמה דקות. אף פעם אל תשמרו שחקן שהנתונים שלו לא נטענו.
בעמוד הזה
איך המלאי בנוי
המערכת מורכבת משישה חלקים קטנים, ולכל אחד משימה אחת. כל הכללים על פריטים חיים במודול שרת אחד, ולכן כשמשהו נשבר, צריך לחפש בדיוק במקום אחד.
| חלק | סוג ומקום | משימה |
|---|---|---|
ItemCatalog | ModuleScript ב-ReplicatedStorage | מה כל פריט: שם, תיאור, ערימה מקסימלית והאם אפשר להצטייד בו או להשתמש בו. השרת וכל הלקוחות קוראים אותו. |
InventoryService | ModuleScript ב-ServerScriptService | מחזיק את המשבצות של כל השחקנים. הקוד היחיד שמוסיף, מסיר ועורם פריטים. |
InventoryData | Script ב-ServerScriptService | טוען את הפריטים של השחקן בכניסה ושומר אותם. |
InventoryActions | Script ב-ServerScriptService | בודק ומבצע בקשות הצטיידות ושימוש. |
ItemPickup | Script בתוך Part | נותן פריט למי שאסף אותו. |
InventoryClient | LocalScript ב-StarterPlayerScripts | מצייר את הרשת ושולח בקשות. בעצמו לא משנה כלום. |
הנתונים זורמים לכיוון אחד. השרת משנה את המלאי, ואז שולח לשחקן הזה InventoryUpdated עם כל המלאי, והלקוח מצייר אותו מחדש. כשהשחקן לוחץ על משבצת, הלקוח שולח EquipItem או UseItem עם ה-id של הפריט; השרת בודק את הבקשה, מבצע את השינוי, והעותק החדש מגיע באותה דרך. לשלוח את כל המלאי פשוט יותר מלשלוח שינויים, ולכמה עשרות משבצות ההודעה נשארת קטנה.
למה הלקוח אף פעם לא מוסיף פריטים
מתחשק להוסיף פריט ישירות ב-LocalScript שמציג אותו: השחקן לחץ, המשבצת התמלאה. זה נשבר בשתי דרכים.
- אף אחד אחר לא רואה את זה. שינוי ב-LocalScript קיים רק במכשיר של השחקן הזה. השרת לא יודע עליו, ולכן הוא לא נשמר, ושום סקריפט שרת לא יכול להשתמש בו.
- כל אחד יכול לזייף. אם מתקנים את זה עם remote כמו
AddItem(itemId, amount), רמאי יוכל להפעיל את האירוע הזה בעצמו, עם כל ארגומנט וכמה פעמים שירצה. במדריך האבטחה של Roblox כתוב שהשרת חייב לבדוק כל מה שהלקוח שולח לפני שהוא משתמש בזה.
לכן במדריך הזה הלקוח יכול רק לבקש לעשות משהו עם פריטים שכבר יש לשחקן: "תצייד אותי בזה" ו"תשתמש בזה". בכל בקשה יש id פריט אחד ובלי כמויות, ובשרת היא עוברת בדיקה אחת: זו מחרוזת, הפריט קיים, לשחקן יש לפחות אחד, והשחקן שולח לא יותר מארבע בקשות בשנייה. אז השרת בעצמו מוצא את ה-Tool לפי הקטלוג. הוא אף פעם לא מקבל מהלקוח Tool, נתיב או instance, כמו שאותו מדריך מזהיר.
פריטים נכנסים למלאי רק דרך קוד שרת: איסוף שהשרת בדק, קנייה שהשרת חייב עליה כסף (המדריך לחנות בונה אותה), פרס על משימה או דרופ. על remotes באופן כללי, עם בדיקה של סוגים אחרים של ארגומנטים, קראו במדריך ל-RemoteEvent.
שלב 1: הכינו את ה-Explorer
צרו את האובייקטים האלה ביד. הסקריפטים מהשלבים הבאים מחפשים אותם לפי שם, אז כתבו כל שם בדיוק כמו כאן.
- ב-ReplicatedStorage הכניסו Folder בשם
InventoryRemotes. בתוכו הכניסו שלושה RemoteEvent בשמותInventoryUpdated,EquipItemו-UseItem. - ב-ServerStorage הכניסו Folder בשם
ItemTools. בתוכו הכניסו Tool בשםSword, וב-Tool Part בשםHandle(1 על 4 על 1 studs מתאים לגמרי). בדקו שהסימון Anchored של ה-Handle מוסר: Roblox מזהירה שהדמות נתקעת במקום אם היא מחזיקה כלי עם Parts מעוגנים. מתאים גם Tool שכבר יש לכם, אם השם שלו תואם לקטלוג משלב 2. - הוסיפו סקריפטים תוך כדי מעבר על השלבים. העץ מראה לאן הולך כל אחד מהם.
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 והחליפו את התוכן שלו בזה:
-- כל הפריטים שיכולים להיות במשחק. את הרשימה הזו קוראים השרת וכל
-- הלקוחות; רק השרת מחליט למי יש מה.
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 רגישים לאותיות גדולות וקטנות, ודווקא הם נשמרים, ולכן אף פעם אל תשנו id כשהפריט כבר אצל שחקנים: שנו את ה-name שלו.
שלב 3: שמרו את המלאים בשרת
ה-ModuleScript הזה הוא הבעלים של המלאי של כל שחקן. כל מלאי הוא רשימת משבצות, בכל משבצת id של פריט וכמות, ועוד קיבולת של 20 משבצות. סקריפטי שרת אחרים קוראים לפונקציות שלו, ואף אחד אחר לא עורך את הטבלה.
הכניסו 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: טענו ושמרו כל מלאי
המלאים חיים בזיכרון של השרת, ולכן נעלמים כשהשחקן יוצא. הסקריפט הזה טוען את המשבצות של השחקן ממאגר הנתונים כשהוא נכנס, וכותב אותן בחזרה כשהוא יוצא, כשהשרת נכבה וכל שלוש דקות למקרה של קריסת שרת. הוא שומר רק id של פריטים וכמות: מאגרי נתונים לא יודעים לשמור Instance, למשל Tool.
הכניסו 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 הוא במקום זה מקבל פריטי התחלה שלא נשמרים, כמה שניות מאוחר יותר בגלל הניסיונות החוזרים. כך יהיה עד שהפלייס יפורסם עם גישה ל-API מופעלת. - שמירה ביציאה ובכיבוי.
BindToCloseשומר את כל מי שעדיין בשרת ומחכה לשמירות שכבר רצות. Roblox נותנת לפונקציות קשורות עד 30 שניות. - לא לחרוג מהמגבלות. כתיבה אחת לשחקן כל שלוש דקות ועוד אחת ביציאה זה הרבה פחות מתקציב השרת הסטנדרטי של 60 כתיבות בדקה ועוד 40 לכל שחקן (לפי התיעוד נכון ל-30 בספטמבר 2026). תבנית המפתח
User_לקוחה מההמלצות של Roblox, והעברת ה-user ID ל-SetAsyncקושרת את הנתונים לשחקן הזה, מה ש-Roblox ממליצה עליו בחום לטיפול בבקשות למחיקת נתונים.
למשחק קטן זה בסיס אמין. נעילת סשן, שמונעת משחקן שהחליף שרת מהר לטעון נתונים לפני שהשרת הישן שמר אותם, ומתי להשתמש ב-UpdateAsync במקום ב-SetAsync, מפורטים במדריך ל-DataStore.
שלב 5: תנו פריטים מהעולם
איסוף הוא הדרך הכי פשוטה שבה פריטים נכנסים למלאי: Part עם ProximityPrompt שמוסיף פריט כששחקן משתמש בו. הסקריפט מאזין לאירוע Triggered שלו בשרת, ולכן הפריט נוסף במקום שבו הלקוח לא יכול להתערב.
- הוסיפו Part ל-Workspace, קראו לו
Appleוצבעו אותו באדום. הפעילו Anchored וכבו CanCollide, כדי שאף אחד לא ייתקל בו בזמן שהוא בלתי נראה. ההנחיה מציגה את שם ה-Part. - כשה-Part מסומן, גללו את חלון Properties עד החלק Attributes ולחצו על +. הוסיפו
ItemIdמסוג string ולחצו על Save, ואז קבעו את הערךapple. באותה דרך הוסיפוAmountמסוג number עם הערך1. - הכניסו Script ל-Part, קראו לו
ItemPickupוהדביקו את הקוד הזה:
-- שימו את ה-Script הזה בתוך Part ותנו ל-Part שני Attributes:
-- 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)
העתיקו את ה-Part כדי לפזר עוד פריטים. שנו את ה-Attributes כדי לתת עץ, עוד חרב או ערימת תפוחים.
Roblox בודקת את המרחק ל-Triggered, אבל מדריך האבטחה שלה עדיין אומר לבדוק הנחיות בשרת: רמאי יכול להפעיל אירועי הנחיה גם כש-Enabled שווה false, ויכול למשוך Part לא מעוגן לדמות שלו. לכן הסקריפט בודק בעצמו את Enabled ואת המרחק, ולכן ה-Part מעוגן.
כל סקריפט שרת אחר נותן פריטים באותה דרך. אחרי קנייה, משימה או הריגה טענו את InventoryService דרך require, כמו הסקריפטים שלמעלה, וקראו ל-InventoryService.add(player, "wood", 5). בדקו קודם את spaceFor כשאסור לאבד את הפריט. מקור נוסף יכול להיות NPC: במדריך לדיאלוג עם NPC תשובה מפעילה פעולה בשרת, והפעולה הזו יכולה לקרוא באותה דרך ל-InventoryService.add.
שלב 6: הצטיידות ב-Tool ושימוש בפריטים
שני RemoteEvent נושאים את הבקשות היחידות שהלקוח יכול לשלוח. EquipItem שם עותק של ה-Tool של הפריט מ-ServerStorage ב-Backpack של השחקן (סרגל הכלים בתחתית המסך) וביד שלו, ובקשה חוזרת מורידה אותו. UseItem מסיר יחידה אחת של פריט שאפשר להשתמש בו ומפעיל את ההשפעה שלו, כאן זה ריפוי.
הכניסו 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 אחרי לידה מחדש
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, שם שחקן אחר יכול לקחת אותו, והמקור בינתיים נשאר במלאי. כל עותק נושא את ה-Attribute בשם InventoryItem, ולכן בהורדה אפשר למצוא ולהשמיד כל עותק, איפה שלא יהיה. ואם לשחקן יש מכל StarterGear, מכניסים אליו עותק שני. Roblox מעתיקה את StarterGear ל-Backpack בכל הופעה של הדמות, ולכן החרב המצוידת חוזרת אחרי לידה מחדש.
רק Tool אחד מצויד בכל פעם, וההצטיידות לא נשמרת, ולכן שחקן שנכנס מחדש מתחיל עם סרגל כלים ריק. אם תוסיפו אחר כך מכירה או זריקה, הורידו את הפריט לפני שהעותק האחרון שלו עוזב את המלאי.
את חרב הדמה אפשר להפוך לחרב שבאמת נלחמת: בנו Tool לפי המדריך לחרב, השאירו לה את השם Sword ושימו אותה ב-ItemTools במקום הדמה. סקריפט השרת מאותו מדריך יבדוק כל מכה, לא משנה איך השחקן קיבל את החרב.
שלב 7: ציירו את רשת המלאי
ה-LocalScript בונה את כל החלון בקוד, ולכן לא צריך לסדר כלום ביד. ScrollingFrame מחזיק את המשבצות, UIGridLayout בתוכו מסדר אותן בשורות, ו-AutomaticCanvasSize מגדיל את האזור הנגלל ככל שנוספות שורות.
ב-Explorer פתחו את StarterPlayer, הכניסו 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 שומר אותו אחרי לידות מחדש.
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 בלידה מחדש של הדמות, ולכן הרשת ממשיכה להציג את העותק האחרון שהשרת שלח.
הלקוח מצייר תא לכל משבצת, כולל ריקות, ולכן מלאי של 20 משבצות תמיד נראה כמו 20 משבצות. כל עדכון משמיד את התאים הישנים ובונה חדשים, וזה פשוט ומתאים לכמה עשרות משבצות. אם השרת ישלח InventoryUpdated לפני שהסקריפט הזה התחבר אליו, Roblox תכניס את ההודעה לתור ותמסור אותה ברגע שה-handler מתחבר, ולכן המלאי הראשון לא ילך לאיבוד.
רוצים לצייר את החלון ב-Studio? בנו ScreenGui עם משבצת תבנית אחת ועשו לה Clone ב-makeCell. לאייקונים קבעו לפריט בקטלוג icon עם תמונה שהעליתם, והתא שלו יציג תמונה במקום שם.
בדקו את המלאי
- לחצו על Play. פתחו את המלאי עם הכפתור משמאל או עם המקש B: לשחקן חדש יש Sword ו-3 Apple, והמונה מציג 2 / 20 slots. כל עוד Studio לא יכול לפנות למאגרי נתונים, הפריטים מופיעים אחרי כמה שניות, אחרי אזהרות טעינה ב-Output, ולא נשמרים.
- לחצו על Sword. החרב תופיע ביד שלכם ובסרגל הכלים, והמשבצת שלה תקבל מסגרת סגולה. לחצו שוב כדי להוריד אותה.
- גשו ל-Part בשם Apple והשתמשו בהנחיה שלו. ערימת התפוחים תגדל ל-4, וה-Part ייעלם ל-15 שניות.
- כדי לבדוק ריפוי, קודם הורידו את החיים. העבירו את Client/Server ל-Server, סמנו את ה-
Humanoidשל הדמות שלכם ב-Explorer, קבעו Health ל-50, חזרו ל-Client ולחצו על Apple. - עם גישת 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 צריך Part בשם Handle כדי שאפשר יהיה להחזיק אותו, אלא אם קבעתם את ה-RequiresHandle שלו ל-false.
הנחיית האיסוף מופיעה, אבל שום דבר לא נוסף
ה-Attribute בשם ItemId חייב להתאים בדיוק ל-id מהקטלוג, כולל אותיות: apple, ולא Apple. שום דבר לא נוסף בזמן שהמלאי של השחקן עדיין נטען, או כשאין מקום לכל הכמות.
השמירה נשברת אחרי ששיניתי מה נשמר
מאגרי נתונים מקבלים נתונים פשוטים: מחרוזות, מספרים, בוליאנים וטבלאות שלהם. שמירה של Tool, של Part או של כל Instance אחר נכשלת, או שהערך נשמר כ-nil. שמרו id של פריט וכמות, ואת כל השאר שחזרו מהקטלוג, כמו ש-serialize עושה.
לעשות עם RoCode
אם קל לכם יותר לתאר מערכת מאשר לבנות אותה ביד, RoCode יכול לבנות אותה בפלייס שפתוח אצלכם. זה סוכן AI ל-Roblox Studio: אתם מתארים פיצ׳ר בצ׳אט באתר, והפלאגין שלו ל-Studio יוצר סקריפטים ואובייקטים ב-Explorer שלכם, ולא נותן קוד להדבקה.
תוסיף מלאי של 20 משבצות עם ערימות פריטים, רשת UI שנפתחת על B, חרב מ-ServerStorage ששחקנים יכולים להצטייד בה, תפוחים שמרפאים ושמירה דרך DataStoreService.
- יכול לסרוק את הפלייס שלכם כששלב צריך את זה, עם Search Game ו-Search Scripts, כדי להישען על פריטים, remotes או קוד שמירה שכבר קיימים, ולא להוסיף מערכת שנייה
- יוצר תיקיות ו-RemoteEvent עם Create Instance, ו-ModuleScript, Script ו-LocalScript עם Create Script
- יכול לבנות שלד של חלון עם Build UI, שיש בו פריסת מלאי עם משבצות דמה; את הקוד שממלא את המשבצות מהשרת כותבים בסקריפטים נפרדים
- בודק את הקומפילציה של כל סקריפט ב-Studio עם Check Script, שתופס שגיאות קומפילציה וכמה דפוסים מסוכנים, אבל לא בודק טיפוסים, לא עושה lint ולא מריץ את הקוד שלכם
כל קבוצת שינויים היא נקודת ביטול ב-Studio. RoCode לא יכול להפעיל את גישת Studio ל-API services, את זה אתם עושים בעצמכם. עם פלאגין 3.9 ומעלה הוא יכול להריץ בדיקת משחק קצרה ולקרוא את השגיאות, ועם פלאגין 3.9.15 ומעלה דמות הבדיקה שלו יכולה ללחוץ על מקשים ולהצטייד בפריטים, אבל הוא בודק רק את מה שהוא עצמו החליט לבדוק, אז את השמירה בדקו בעצמכם בבדיקת משחק. המלאי יושב מעל מערכות אחרות, כמו שמירה וכלים, ויוצר המשחקים עם AI שבונה בפלייס שלכם ב-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.
איך גורמים למשבצות לשמור מיקום בשביל גרירה ושחרור?
שמרו רשימה באורך קבוע עם רשומה אחת לכל משבצת, וסמנו משבצות ריקות בערך כמו id ריק, ולא nil, כי Roblox לא ממליצה לשלוח טבלאות עם nil דרך RemoteEvent. אחר כך הוסיפו RemoteEvent בשם MoveItem ששולח שני מספרי משבצות, ותנו לשרת לבדוק ששניהם מספרים שלמים בין 1 לקיבולת לפני שהוא מחליף ביניהם.
כמה גדול יכול להיות מלאי שמור?
ערך אחד במאגר נתונים יכול לתפוס עד 4,194,304 תווים אחרי סריאליזציה (לפי התיעוד נכון ל-30 בספטמבר 2026). רשימת id וכמויות תופסת חלק זעיר מזה. את גודל השמירה מגדיל טקסט לכל פריט, למשל שמות משלכם או תיאורים ארוכים שנשמרים לכל פריט.
איך קונים ומוכרים פריטים תמורת מטבעות?
עשו הכול בשרת. כדי למכור, קראו ל-InventoryService.remove ושלמו רק אם הוא החזיר true. כדי לקנות, בדקו את המחיר ואת spaceFor, קחו את המטבעות, ואז קראו ל-add. המדריך לחנות בונה את צד הקנייה.
מקורות וקריאה נוספת
- Roblox Creator Docs: Remote events and callbacks (באנגלית). FireClient, OnServerEvent, הגבלות על ארגומנטים ואיך הודעות בלי נמען נכנסות לתור
- Roblox Creator Docs: Securing the client-server boundary (באנגלית). בדיקת קלט של remotes, בדיקות ProximityPrompt וזיוף נתונים במאגר הנתונים
- Roblox Creator Docs: Data stores (באנגלית). GetAsync, SetAsync והפעלת גישה מ-Studio
- Roblox Creator Docs: Best practices for data stores (באנגלית). מפתח אחד לכל שחקן, תבניות מפתח ושמירות תקופתיות
- Roblox Creator Docs: Data store error codes and limits (באנגלית). מגבלות בקשות לשרת ומגבלת הגודל של ערך אחד
- Roblox Creator Docs: In-game tools (באנגלית). Handle, עיגון, Backpack ו-StarterGear
- Roblox Creator Docs: Grid and table layouts (באנגלית). גודל התאים ב-UIGridLayout, מרווחים וסדר המיון
איך הקוד נבדק: כל סקריפט בעמוד הזה עובר את בודק הטיפוסים המחמיר של Luau עם הגדרות ה-API של Roblox (luau-lsp, 20 בנובמבר 2026). בדיקת טיפוסים תופסת שגיאות בשמות API וטיפוסים שגויים, אבל לא שגיאות בלוגיקת המשחק, אז בדקו הכול ב-Studio לפני הפרסום.