संक्षेप में
हर प्लेयर के आइटम सर्वर पर एक टेबल में रखें, ServerScriptService के ModuleScript के अंदर, और सिर्फ़ यही कोड उन्हें जोड़, हटा और स्टैक कर सकता है। आइटम एक बार ReplicatedStorage के ModuleScript में बताएँ (id, नाम, अधिकतम स्टैक)। हर बदलाव के बाद सर्वर उस प्लेयर को उसकी इन्वेंटरी की कॉपी के साथ एक RemoteEvent भेजता है, और एक LocalScript उसे ScrollingFrame और UIGridLayout से ग्रिड में दिखाता है।
- क्लाइंट कभी आइटम नहीं जोड़ता। वह सिर्फ़ माँग सकता है, जैसे "मेरी तलवार equip करो", और सर्वर कुछ करने से पहले जाँचता है कि प्लेयर के पास वह है।
- आइटम सिर्फ़ उस सर्वर कोड से आते हैं जिसे आप कंट्रोल करते हैं: उठाना, सर्वर पर जाँच के बाद शॉप से ख़रीदारी, इनाम।
- Tool नहीं, आइटम की id और उनकी संख्या DataStoreService से सेव करें, जब प्लेयर जाए, जब सर्वर बंद हो और हर कुछ मिनट में। जिस प्लेयर का डेटा लोड नहीं हुआ, उसे कभी सेव न करें।
इस पेज पर
इन्वेंटरी कैसे बनी होती है
सिस्टम छह छोटे हिस्सों से बना है, हर एक का एक काम। आइटम के सब नियम एक सर्वर मॉड्यूल में रहते हैं, इसलिए जब कुछ टूटे तो ठीक एक जगह ढूँढना होता है।
| हिस्सा | टाइप और जगह | काम |
|---|---|---|
ItemCatalog | ReplicatedStorage में ModuleScript | हर आइटम क्या है: नाम, विवरण, अधिकतम स्टैक, और क्या उसे equip या इस्तेमाल किया जा सकता है। इसे सर्वर और सब क्लाइंट पढ़ते हैं। |
InventoryService | ServerScriptService में ModuleScript | सब प्लेयर्स के स्लॉट रखता है। इकलौता कोड जो आइटम जोड़ता, हटाता और स्टैक करता है। |
InventoryData | ServerScriptService में Script | प्लेयर के आने पर उसके आइटम लोड करता है और उन्हें सेव करता है। |
InventoryActions | ServerScriptService में Script | equip और इस्तेमाल के अनुरोध जाँचता और पूरे करता है। |
ItemPickup | Part के अंदर Script | जिसने आइटम उठाया उसे आइटम देता है। |
InventoryClient | StarterPlayerScripts में LocalScript | ग्रिड दिखाता है और अनुरोध भेजता है। ख़ुद कुछ नहीं बदलता। |
डेटा एक दिशा में चलता है। सर्वर इन्वेंटरी बदलता है, फिर उस प्लेयर को पूरी इन्वेंटरी के साथ InventoryUpdated भेजता है, और क्लाइंट उसे फिर से दिखाता है। जब प्लेयर किसी स्लॉट पर क्लिक करता है, क्लाइंट आइटम की id के साथ EquipItem या UseItem भेजता है; सर्वर अनुरोध जाँचता है, बदलाव करता है, और नई कॉपी उसी रास्ते से आती है। बदलाव भेजने से पूरी इन्वेंटरी भेजना आसान है, और कुछ दर्जन स्लॉट के लिए संदेश छोटा रहता है।
क्लाइंट कभी आइटम क्यों नहीं जोड़ता
मन करता है कि आइटम सीधे उसी LocalScript में जोड़ दें जो उसे दिखाता है: प्लेयर ने क्लिक किया, स्लॉट भर गया। यह दो तरह से टूटता है।
- कोई और इसे नहीं देखता। LocalScript का बदलाव सिर्फ़ उसी प्लेयर के डिवाइस पर होता है। सर्वर को उसका पता नहीं चलता, इसलिए वह सेव नहीं होता, और कोई सर्वर स्क्रिप्ट उसे इस्तेमाल नहीं कर सकती।
- कोई भी नक़ली बना सकता है। अगर इसे
AddItem(itemId, amount)जैसे remote से ठीक करें, तो चीटर इस इवेंट को ख़ुद, किसी भी arguments के साथ और जितनी बार चाहे कॉल कर सकेगा। Roblox की सुरक्षा गाइड कहती है कि सर्वर को क्लाइंट की भेजी हर चीज़ इस्तेमाल करने से पहले जाँचनी चाहिए।
इसलिए इस गाइड में क्लाइंट सिर्फ़ उन आइटम के साथ कुछ करने को कह सकता है जो प्लेयर के पास पहले से हैं: "इसे equip करो" और "इसे इस्तेमाल करो"। हर अनुरोध में एक आइटम id होती है और कोई संख्या नहीं, और सर्वर पर वह एक जाँच से गुज़रता है: यह स्ट्रिंग है, ऐसा आइटम मौजूद है, प्लेयर के पास कम से कम एक है, और प्लेयर प्रति सेकंड चार से ज़्यादा अनुरोध नहीं भेजता। फिर सर्वर ख़ुद कैटलॉग से Tool ढूँढता है। वह क्लाइंट से कभी Tool, path या instance नहीं लेता, जिसके बारे में वही गाइड चेतावनी देती है।
आइटम इन्वेंटरी में सिर्फ़ सर्वर कोड से आते हैं: ऐसा उठाना जिसे सर्वर ने जाँचा, ऐसी ख़रीद जिसके पैसे सर्वर ने काटे (इसे शॉप गाइड बनाती है), क्वेस्ट या ड्रॉप का इनाम। Remotes के बारे में आम तौर पर, दूसरी तरह के arguments की जाँच के साथ, RemoteEvent गाइड में पढ़ें।
स्टेप 1: Explorer तैयार करें
ये ऑब्जेक्ट हाथ से बनाएँ। अगले स्टेप्स की स्क्रिप्ट इन्हें नाम से ढूँढती हैं, इसलिए हर नाम ठीक यहाँ जैसा लिखें।
- ReplicatedStorage में
InventoryRemotesनाम का Folder डालें। उसके अंदरInventoryUpdated,EquipItemऔरUseItemनाम के तीन RemoteEvent डालें। - ServerStorage में
ItemToolsनाम का Folder डालें। उसके अंदरSwordनाम का Tool डालें, और Tool मेंHandleनाम का Part (1 गुणा 4 गुणा 1 stud काफ़ी है)। जाँचें कि Handle पर Anchored बंद है: Roblox चेतावनी देता है कि anchored पार्ट्स वाला टूल पकड़ने पर कैरेक्टर जगह पर अटक जाता है। आपके पास पहले से मौजूद 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 और संख्या रखती हैं। इससे सेव छोटे रहते हैं, और बाद में किसी का सेव डेटा छुए बिना आइटम का नाम बदला या उसका विवरण फिर से लिखा जा सकता है।
ReplicatedStorage में एक ModuleScript डालें, उसका नाम ItemCatalog रखें और उसका कंटेंट इससे बदल दें:
-- गेम में हो सकने वाले सब आइटम। यह लिस्ट सर्वर और सब
-- क्लाइंट पढ़ते हैं; किसके पास क्या है, यह सिर्फ़ सर्वर तय करता है।
export type Item = {
id: string, -- नीचे की key से मेल खाना चाहिए
name: string,
description: string,
maxStack: number, -- इस आइटम के कितने एक स्लॉट में समाते हैं
icon: string?, -- वैकल्पिक तस्वीर: "rbxassetid://" और आपकी इमेज की ID
toolName: string?, -- ServerStorage.ItemTools का Tool: आइटम equip किया जा सकता है
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
वैकल्पिक fields तय करते हैं कि आइटम क्या करता है। toolName उसे equip करने लायक़ बनाता है, heal इस्तेमाल करने लायक़, और दोनों fields के बिना आइटम, जैसे लकड़ी, इन्वेंटरी में तब तक पड़ा रहता है जब तक कोई दूसरा सर्वर कोड (crafting, बिक्री) उसे न हटाए।
आइटम जोड़ने के लिए ऐसी एंट्री जोड़ें जिसकी key और id एक जैसी हों। Id केस-सेंसिटिव हैं और यही सेव होती हैं, इसलिए जब आइटम प्लेयर्स के पास हो तो उसकी id कभी न बदलें: उसका name बदलें।
स्टेप 3: इन्वेंटरी सर्वर पर रखें
यह ModuleScript हर प्लेयर की इन्वेंटरी का मालिक है। हर इन्वेंटरी स्लॉट की एक लिस्ट है, हर स्लॉट में आइटम id और संख्या, साथ में 20 स्लॉट की क्षमता। दूसरी सर्वर स्क्रिप्ट इसके फ़ंक्शन कॉल करती हैं, और कोई और टेबल नहीं बदलता।
ServerScriptService में एक ModuleScript डालें और उसका नाम InventoryService रखें:
-- इन्वेंटरी बदलने वाला इकलौता कोड। यह ServerScriptService में है,
-- जहाँ क्लाइंट इसे न देख सकते हैं न चला सकते हैं।
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Strict मोड नहीं देख पाता कि WaitForChild से require क्या लौटाता है, इसलिए
-- मॉड्यूल का वह हिस्सा बताते हैं जो यह स्क्रिप्ट इस्तेमाल करती है
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 वह है जो
-- DataStore ने लौटाया: नए प्लेयर के लिए 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
require के बाद की type लाइनें और :: cast सिर्फ़ Luau के type checker के लिए हैं, जो यह नहीं देख पाता कि WaitForChild से मिला मॉड्यूल क्या लौटाता है। गेम के दौरान वे कुछ नहीं बदलतीं। इस गाइड की बाक़ी स्क्रिप्ट भी ऐसा ही करती हैं।
ऊपर के कैटलॉग के साथ, जहाँ सेब 10 तक स्टैक होते हैं, स्टैक के नियम कैसे काम करते हैं:
- अगर 7 के स्टैक वाले प्लेयर को 15 सेब दें, तो वह स्टैक 10 तक भरेगा, और फिर 5 का नया स्टैक शुरू होगा।
- जब स्लॉट ख़त्म हो जाते हैं,
addजितना समाया उतना जोड़ता है और वह संख्या लौटाता है। जब सब या कुछ नहीं चाहिए, तो पहलेspaceForकॉल करें, जैसे स्टेप 5 में उठाना करता है। removeसब या कुछ नहीं के सिद्धांत पर चलता है। जिसके पास 2 सेब हैं उससे 3 माँगने पर कुछ नहीं हटता औरfalseलौटता है, इसलिए बदले में कुछ देने से पहले नतीजा जाँचें।- स्लॉट में ख़ाली जगह नहीं रहती: जब कोई स्लॉट ख़ाली होता है, बाक़ी ऊपर खिसक जाते हैं। इससे लिस्ट भेजना और सेव करना आसान है। अपनी जगह बनाए रखने वाले स्लॉट के बारे में आख़िर के सवालों में बताया गया है।
| फ़ंक्शन | लौटाता है | किसलिए |
|---|---|---|
add(player, itemId, amount) | कितने जोड़े गए | उठाना, इनाम, ख़रीदारी |
remove(player, itemId, amount) | true, अगर सब हटा दिया गया | इस्तेमाल, बिक्री, crafting |
count(player, itemId) | सब स्टैक का जोड़ | जाँचना कि प्लेयर के पास क्या है |
spaceFor(player, itemId) | और कितने समाएँगे | सब या कुछ नहीं वाले जोड़ने से पहले जाँच |
getEquipped, setEquipped | equip किए आइटम की id या nil | Equip, स्टेप 6 में |
load, serialize, unload | सेव के लिए तैयार कॉपी (serialize) | लोड और सेव, स्टेप 4 में |
स्टेप 4: हर इन्वेंटरी लोड और सेव करें
इन्वेंटरी सर्वर की मेमोरी में रहती हैं, इसलिए प्लेयर के जाने पर ग़ायब हो जाती हैं। यह स्क्रिप्ट प्लेयर के आने पर उसके स्लॉट DataStore से लोड करती है, और उसके जाने पर, सर्वर बंद होने पर और सर्वर क्रैश के डर से हर तीन मिनट में उन्हें वापस लिखती है। यह सिर्फ़ आइटम id और संख्या सेव करती है: DataStore Instance, जैसे Tool, नहीं रख सकते।
ServerScriptService में एक Script डालें और उसका नाम 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 के अंदर है, इसलिए DataStore के एक्सेस के बिना
-- 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
स्क्रिप्ट क्या करती है और क्यों:
- डिफ़ॉल्ट वैल्यू के साथ लोड। नए प्लेयर की कोई सेव वैल्यू नहीं होती, इसलिए
GetAsyncnil लौटाता है, और उसे शुरुआती आइटम मिलते हैं। सेव किए स्लॉट फिर से स्टैक के नियमों से गुज़रते हैं, इसलिए कैटलॉग से हटाया गया आइटम या घटाया गया अधिकतम स्टैक लोड नहीं तोड़ेगा। जो अब नहीं समाता या जिसकी id कैटलॉग में नहीं रही, वह छोड़ दिया जाता है, और अगला सेव इसे पक्का कर देता है, इसलिए कैटलॉग में हर वह id रखें जो प्लेयर्स के पास हो सकती है। - फ़ेल लोड के बाद कभी सेव नहीं। सिर्फ़
canSaveवाले प्लेयर लिखे जाते हैं। लाइव सर्वर पर जिस प्लेयर का डेटा तीन कोशिशों के बाद भी लोड नहीं हुआ, उसे फिर से जुड़ने को कहा जाता है। Studio में उसे इसकी जगह सेव न होने वाले शुरुआती आइटम मिलते हैं, दोबारा कोशिशों की वजह से कुछ सेकंड देर से। ऐसा तब तक होगा जब तक place API एक्सेस चालू करके पब्लिश नहीं होता। - जाने पर और बंद होने पर सेव।
BindToCloseसर्वर पर अभी मौजूद सबको सेव करता है और चल रहे सेव का इंतज़ार करता है। Roblox बँधे फ़ंक्शन को 30 सेकंड तक देता है। - सीमाओं से बाहर न जाना। हर तीन मिनट में प्रति प्लेयर एक write और जाने पर एक, सर्वर के मानक बजट, प्रति मिनट 60 writes और हर प्लेयर पर 40 (30 सितंबर 2026 के डॉक्यूमेंटेशन के अनुसार), से बहुत कम है।
User_key पैटर्न Roblox की सलाह से लिया गया है, औरSetAsyncमें user ID देने से डेटा उस प्लेयर से जुड़ता है, जिसकी Roblox डेटा हटाने के अनुरोध संभालने के लिए ज़ोरदार सलाह देता है।
छोटे गेम के लिए यह मज़बूत आधार है। Session locking, जो जल्दी सर्वर बदलने वाले प्लेयर को पुराने सर्वर के सेव करने से पहले डेटा लोड करने से रोकता है, और SetAsync की जगह UpdateAsync कब इस्तेमाल करें, यह DataStore ट्यूटोरियल में समझाया गया है।
स्टेप 5: दुनिया से आइटम दें
उठाना इन्वेंटरी में आइटम आने का सबसे आसान तरीक़ा है: ProximityPrompt वाला एक पार्ट, जो प्लेयर के इस्तेमाल करने पर आइटम जोड़ता है। स्क्रिप्ट उसका Triggered इवेंट सर्वर पर सुनती है, इसलिए आइटम वहाँ जुड़ता है जहाँ क्लाइंट दख़ल नहीं दे सकता।
- Workspace में एक Part जोड़ें, उसका नाम
Appleरखें और लाल रंग दें। Anchored चालू करें और CanCollide बंद करें, ताकि अदृश्य होने पर कोई उससे न टकराए। प्रॉम्प्ट पार्ट का नाम दिखाता है। - पार्ट चुने रहते Properties विंडो में Attributes हिस्से तक स्क्रॉल करें और + दबाएँ। string टाइप का
ItemIdजोड़ें और Save दबाएँ, फिर वैल्यूappleरखें। इसी तरह number टाइप काAmountवैल्यू1के साथ जोड़ें। - पार्ट में एक Script डालें, उसका नाम
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)
ज़्यादा आइटम रखने के लिए पार्ट कॉपी करें। लकड़ी, एक और तलवार या सेबों का स्टैक देने के लिए attributes बदलें।
Roblox Triggered के लिए दूरी जाँचता है, पर उसकी सुरक्षा गाइड फिर भी प्रॉम्प्ट को सर्वर पर जाँचने को कहती है: चीटर Enabled के false होने पर भी प्रॉम्प्ट इवेंट चला सकता है, और बिना anchor वाले पार्ट को अपने कैरेक्टर की ओर खींच सकता है। इसलिए स्क्रिप्ट ख़ुद Enabled और दूरी जाँचती है, और इसलिए पार्ट anchored है।
कोई भी दूसरी सर्वर स्क्रिप्ट आइटम इसी तरह देती है। ख़रीद, क्वेस्ट या kill के बाद ऊपर की स्क्रिप्ट्स की तरह InventoryService को require से जोड़ें और InventoryService.add(player, "wood", 5) कॉल करें। जब आइटम खोया नहीं जा सकता, तो पहले spaceFor जाँचें। एक और स्रोत NPC हो सकता है: NPC डायलॉग गाइड में जवाब सर्वर पर एक एक्शन चलाता है, और वह एक्शन भी इसी तरह InventoryService.add कॉल कर सकता है।
स्टेप 6: Tool equip करना और आइटम इस्तेमाल करना
दो RemoteEvent वे इकलौते अनुरोध ले जाते हैं जो क्लाइंट कर सकता है। EquipItem ServerStorage से आइटम के Tool की कॉपी प्लेयर के Backpack (स्क्रीन के नीचे का हॉटबार) में और उसके हाथ में रखता है, और दूसरा अनुरोध उसे हटा देता है। UseItem इस्तेमाल होने वाले आइटम का एक नग हटाता है और उसका असर लागू करता है, यहाँ हील।
ServerScriptService में एक Script डालें और उसका नाम 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 destroy करता है, कॉपी कहीं भी हो: हॉटबार, हाथ या 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) -- एक समय में एक equip किया टूल
local tool = template:Clone()
tool.CanBeDropped = false -- गिराई गई कॉपी कोई और उठा सकता था
tool:SetAttribute("InventoryItem", item.id)
tool.Parent = backpack
-- StarterGear की दूसरी कॉपी respawn के बाद वापस 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) -- equip किए आइटम पर फिर से क्लिक करने से वह हटता है
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 attribute है, इसलिए unequip पर हर कॉपी ढूँढकर destroy की जा सकती है, वह कहीं भी हो। और अगर प्लेयर के पास StarterGear कंटेनर है, तो उसमें दूसरी कॉपी रखी जाती है। Roblox हर कैरेक्टर spawn पर StarterGear को Backpack में कॉपी करता है, इसलिए equip की गई तलवार respawn के बाद लौट आती है।
एक समय में सिर्फ़ एक Tool equip होता है, और equip सेव नहीं होता, इसलिए फिर से जुड़ने पर प्लेयर ख़ाली हॉटबार से शुरू करता है। अगर बाद में बिक्री या गिराना जोड़ें, तो आइटम की आख़िरी कॉपी इन्वेंटरी छोड़ने से पहले उसे unequip करें।
नक़ली तलवार को सच में लड़ने वाली तलवार बनाया जा सकता है: तलवार गाइड से Tool बनाएँ, उसका नाम Sword रहने दें और नक़ली की जगह ItemTools में रखें। उस गाइड की सर्वर स्क्रिप्ट हर वार जाँचेगी, प्लेयर को तलवार कैसे भी मिली हो।
स्टेप 7: इन्वेंटरी ग्रिड बनाएँ
LocalScript पूरी विंडो कोड में बनाता है, इसलिए हाथ से कुछ रखने की ज़रूरत नहीं। ScrollingFrame स्लॉट रखता है, उसके अंदर UIGridLayout उन्हें पंक्तियों में सजाता है, और AutomaticCanvasSize पंक्तियाँ जुड़ने के साथ स्क्रॉल होने वाला हिस्सा बढ़ाता है।
Explorer में StarterPlayer खोलें, StarterPlayerScripts में एक LocalScript डालें और उसका नाम 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)
StarterPlayerScripts का LocalScript हर सेशन में एक बार चलता है, और ResetOnSpawn = false कैरेक्टर के respawn पर ScreenGui बनाए रखता है, इसलिए ग्रिड सर्वर की भेजी आख़िरी कॉपी दिखाता रहता है।
क्लाइंट हर स्लॉट के लिए एक सेल बनाता है, ख़ाली स्लॉट समेत, इसलिए 20 स्लॉट की इन्वेंटरी हमेशा 20 स्लॉट जैसी दिखती है। हर अपडेट पुराने सेल destroy करके नए बनाता है, यह आसान है और कुछ दर्जन स्लॉट के लिए ठीक है। अगर सर्वर इस स्क्रिप्ट के जुड़ने से पहले InventoryUpdated भेज दे, तो Roblox संदेश को कतार में रखेगा और हैंडलर जुड़ते ही पहुँचा देगा, इसलिए पहली इन्वेंटरी नहीं खोएगी।
विंडो Studio में बनाना चाहते हैं? एक टेम्पलेट स्लॉट वाला ScreenGui बनाएँ और makeCell में उसका Clone करें। आइकन के लिए कैटलॉग में आइटम को अपनी अपलोड की गई इमेज के साथ icon दें, और उसका सेल नाम की जगह तस्वीर दिखाएगा।
इन्वेंटरी टेस्ट करें
- Play दबाएँ। बाईं ओर के बटन या B से इन्वेंटरी खोलें: नए प्लेयर के पास Sword और 3 Apple हैं, और काउंटर 2 / 20 slots दिखाता है। जब तक Studio DataStore तक नहीं पहुँच सकता, आइटम Output में लोड की चेतावनियों के बाद कुछ सेकंड में आते हैं, और सेव नहीं होते।
- Sword पर क्लिक करें। तलवार आपके हाथ में और हॉटबार में आएगी, और उसके स्लॉट को बैंगनी फ़्रेम मिलेगा। उसे हटाने के लिए फिर क्लिक करें।
- Apple पार्ट के पास जाएँ और उसका प्रॉम्प्ट इस्तेमाल करें। सेबों का स्टैक 4 हो जाएगा, और पार्ट 15 सेकंड के लिए ग़ायब हो जाएगा।
- हील जाँचने के लिए पहले हेल्थ घटाएँ। Client/Server को Server पर करें, Explorer में अपने कैरेक्टर का
Humanoidचुनें, Health 50 करें, वापस Client पर जाएँ और Apple पर क्लिक करें। - Studio का API services एक्सेस चालू रखकर गेम रोकें और फिर चलाएँ। उठाया गया सेब अपनी जगह है। अगर लोड या सेव फ़ेल हुआ, तो Output बताएगा।
कई प्लेयर्स के साथ जाँचने के लिए टेस्ट सेटिंग्स में 2 क्लाइंट के साथ Server & Clients चुनें। हर प्लेयर सिर्फ़ अपनी इन्वेंटरी देखता है, और दूसरे के हाथ की तलवार दोनों देखते हैं।
आम गलतियाँ और उनके हल
ज़्यादातर समस्याएँ Output विंडो में चेतावनी या लाल एरर लाइन के रूप में दिखती हैं। पहली पढ़ें: अगले एरर अक्सर उसी से निकलते हैं।
WaitForChild कॉल पर Infinite yield possible
स्क्रिप्ट ऐसे ऑब्जेक्ट का इंतज़ार कर रही है जो ठीक उसी नाम से मौजूद नहीं। अपने Explorer की तुलना स्टेप 1 के ट्री से करें: ReplicatedStorage में तीन RemoteEvent के साथ InventoryRemotes, ServerStorage में ItemTools और दो मॉड्यूल। नाम केस-सेंसिटिव हैं। स्क्रिप्ट एरर गाइड इस चेतावनी को और विस्तार से समझाती है।
ग्रिड खुलता है, पर ख़ाली रहता है
क्लाइंट सिर्फ़ वही दिखाता है जो सर्वर भेजता है। Output में Inventory load failed चेतावनियाँ ढूँढें और जाँचें कि InventoryData ServerScriptService में एक Script है, LocalScript नहीं, और उसके पास InventoryService ModuleScript है। अगर Output Remote event invocation एरर दिखाता है, तो क्लाइंट पर कोई InventoryUpdated नहीं सुन रहा: InventoryClient का पहले वाला एरर ढूँढें।
हर बार Studio में खेलने पर आइटम रीसेट हो जाते हैं
अगर Output Inventory not loaded: playing with starter items that will not be saved लिखता है, तो place पब्लिश नहीं हुआ या Studio का API services एक्सेस बंद है। इसे स्टेप 4 में बताए तरीक़े से, अपने गेम की टेस्ट कॉपी में चालू करें।
तलवार पर क्लिक करने से कुछ नहीं होता
Output में Cannot equip sword ढूँढें: ServerStorage.ItemTools में Tool का नाम ठीक कैटलॉग के toolName जैसा होना चाहिए। साथ ही Tool को पकड़ने के लिए Handle नाम का पार्ट चाहिए, जब तक आपने उसका RequiresHandle false न किया हो।
उठाने का प्रॉम्प्ट दिखता है, पर कुछ नहीं जुड़ता
ItemId attribute केस समेत कैटलॉग की id से हूबहू मेल खाना चाहिए: apple, Apple नहीं। जब तक प्लेयर की इन्वेंटरी लोड हो रही है, या जब पूरी संख्या के लिए जगह नहीं है, कुछ नहीं जुड़ता।
जो सेव होता है उसे बदलने के बाद सेविंग टूट गई
DataStore सादा डेटा लेते हैं: स्ट्रिंग, संख्याएँ, boolean और इनसे बनी टेबल। Tool, Part या किसी और Instance को सेव करना फ़ेल होता है, या वैल्यू nil के रूप में सेव होती है। आइटम id और संख्या सेव करें, और बाक़ी सब कैटलॉग से फिर बनाएँ, जैसे serialize करता है।
RoCode के साथ बनाएँ
अगर आपके लिए सिस्टम बताना हाथ से बनाने से आसान है, तो RoCode इसे आपके खुले place में बना सकता है। यह Roblox Studio के लिए एक AI एजेंट है: आप वेब चैट में फ़ीचर बताते हैं, और उसका Studio प्लगइन आपके Explorer में स्क्रिप्ट और ऑब्जेक्ट बनाता है, पेस्ट करने के लिए कोड नहीं देता।
आइटम स्टैक के साथ 20 स्लॉट की इन्वेंटरी जोड़ो, B पर खुलने वाला ग्रिड UI, ServerStorage से तलवार जिसे प्लेयर्स equip कर सकें, हील करने वाले सेब, और DataStoreService से सेविंग।
- ज़रूरत पड़ने पर Search Game और Search Scripts से आपका place देख सकता है, ताकि दूसरा सिस्टम जोड़ने की जगह पहले से मौजूद आइटम, remotes या सेविंग कोड पर टिके
- Create Instance से फ़ोल्डर और RemoteEvent बनाता है, और Create Script से ModuleScript, Script और LocalScript
- Build UI से विंडो का ढाँचा बना सकता है, जिसमें नक़ली स्लॉट वाला इन्वेंटरी लेआउट है; सर्वर से स्लॉट भरने वाला कोड अलग स्क्रिप्ट में लिखा जाता है
- Studio में Check Script से हर स्क्रिप्ट का कंपाइल जाँचता है, जो कंपाइल एरर और कुछ जोखिम भरे पैटर्न पकड़ता है, पर टाइप नहीं जाँचता, lint नहीं करता और आपका कोड नहीं चलाता
बदलावों का हर बैच Studio में एक undo पॉइंट है। RoCode Studio का API services एक्सेस चालू नहीं कर सकता, यह आप ख़ुद करते हैं। प्लगइन 3.9 या नए के साथ वह छोटा playtest चला सकता है और एरर पढ़ सकता है, और प्लगइन 3.9.15 या नए के साथ उसका टेस्ट कैरेक्टर बटन दबा सकता है और आइटम equip कर सकता है, पर वह सिर्फ़ वही जाँचता है जो उसने ख़ुद टेस्ट करना तय किया, इसलिए सेविंग playtest से ख़ुद जाँचें। इन्वेंटरी दूसरे सिस्टम, जैसे सेविंग और टूल, के ऊपर बैठती है, और AI गेम बिल्डर जो Studio में आपके अपने place में बनाता है, उन्हें एक बार में एक प्रॉम्प्ट से जोड़ता है। RoCode Studio से कैसे जुड़ता है।
अक्सर पूछे जाने वाले सवाल
क्या अपनी इन्वेंटरी की जगह Roblox का Backpack इस्तेमाल करें?
Backpack Roblox की बनी-बनाई इन्वेंटरी है: उसमें हर Tool हॉटबार में एक बटन के रूप में दिखता है। अगर आपके गेम के सब आइटम Tool हैं और आपको स्टैक, संख्या और स्लॉट की सीमा नहीं चाहिए, तो यह काफ़ी हो सकता है, हालाँकि हर प्लेयर के पास कौन से Tool हैं यह फिर भी सेव करना होगा। ऐसे आइटम के लिए अपनी इन्वेंटरी चाहिए जो Tool नहीं हैं, जैसे सामग्री और ख़र्च होने वाली चीज़ें। यह गाइड दोनों इस्तेमाल करती है: इन्वेंटरी सब कुछ रखती है, और Backpack equip किया Tool रखता है।
Roblox में डिफ़ॉल्ट हॉटबार कैसे छिपाएँ?
LocalScript से StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) कॉल करें। हॉटबार छिपा होने पर प्लेयर्स को equip किया Tool लेने और हटाने का दूसरा तरीक़ा चाहिए, जैसे एक बटन जो सर्वर से Humanoid:EquipTool या Humanoid:UnequipTools कॉल करने को कहे।
Drag and drop के लिए स्लॉट अपनी जगह कैसे बनाए रखें?
हर स्लॉट के लिए एक एंट्री वाली तय लंबाई की लिस्ट रखें और ख़ाली स्लॉट को nil की जगह ख़ाली id जैसी वैल्यू से चिह्नित करें, क्योंकि Roblox RemoteEvent से nil वाली टेबल भेजने की सलाह नहीं देता। फिर एक MoveItem RemoteEvent जोड़ें जो दो स्लॉट नंबर भेजे, और सर्वर उन्हें बदलने से पहले जाँचे कि दोनों 1 से क्षमता तक के पूर्णांक हैं।
सेव की गई इन्वेंटरी कितनी बड़ी हो सकती है?
DataStore की एक वैल्यू serialize होने के बाद 4,194,304 अक्षरों तक हो सकती है (30 सितंबर 2026 के डॉक्यूमेंटेशन के अनुसार)। id और संख्याओं की लिस्ट इसका बहुत छोटा हिस्सा लेती है। सेव का साइज़ हर आइटम का टेक्स्ट बढ़ाता है, जैसे अपने नाम या हर आइटम के लिए सेव किए लंबे विवरण।
सिक्कों से आइटम कैसे ख़रीदें और बेचें?
सब कुछ सर्वर पर करें। बेचने के लिए InventoryService.remove कॉल करें और तभी पैसे दें जब उसने true लौटाया हो। ख़रीदने के लिए कीमत और spaceFor जाँचें, सिक्के लें, फिर add कॉल करें। शॉप गाइड ख़रीद वाला हिस्सा बनाती है।
स्रोत और आगे पढ़ें
- Roblox Creator Docs: Remote events and callbacks (अंग्रेज़ी में). FireClient, OnServerEvent, arguments की सीमाएँ और बिना पाने वाले संदेश कैसे कतार में जाते हैं
- Roblox Creator Docs: Securing the client-server boundary (अंग्रेज़ी में). remotes के इनपुट की जाँच, ProximityPrompt की जाँचें और DataStore में डेटा से छेड़छाड़
- Roblox Creator Docs: Data stores (अंग्रेज़ी में). GetAsync, SetAsync और Studio से एक्सेस चालू करना
- Roblox Creator Docs: Best practices for data stores (अंग्रेज़ी में). हर प्लेयर पर एक key, key पैटर्न और समय-समय पर सेव
- Roblox Creator Docs: Data store error codes and limits (अंग्रेज़ी में). हर सर्वर पर अनुरोधों की सीमा और एक वैल्यू के साइज़ की सीमा
- Roblox Creator Docs: In-game tools (अंग्रेज़ी में). Handle, anchor, Backpack और StarterGear
- Roblox Creator Docs: Grid and table layouts (अंग्रेज़ी में). UIGridLayout के सेल का साइज़, padding और sort order
कोड की जाँच कैसे हुई: इस पेज की हर स्क्रिप्ट Roblox API परिभाषाओं के साथ Luau की strict type checking से गुज़रती है (luau-lsp, 20 नवंबर 2026)। Type checking API नामों की गलतियाँ और गलत टाइप पकड़ लेती है, लेकिन गेम लॉजिक की गलतियाँ नहीं, इसलिए पब्लिश करने से पहले सब कुछ Studio में टेस्ट करें।