Right so I am trying to create a backpack system that checks if the player has any inventory data and retrieves the data from the server through a remote function, bare in mind there is going to be a lot more code in the script.
The code just won’t play through, no printing or anything!
I have tried typing lua local itemsInInventory = nil or returnPlayerInventoryData()
as it should be trying to use the function to return a table if the data had been found. However, I get a syntax saying that specific variable trying to call a nil value.
I might be lacking in logic or knowledge, can you guys help me find the issue?
Module (local script):
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local RemotesFolder = ReplicatedStorage:WaitForChild("Remotes")
local rf_inventoryinfo = RemotesFolder:WaitForChild("InventoryInfo")
local re_setinventorydata = RemotesFolder:WaitForChild("SetInventoryData")
local player = players.LocalPlayer
local BackpackService = {}
local slotsInBackpack = nil -- table
local itemsInInventory = nil -- table
local inventoryDataReturned = nil
-- (bool) if the data returned is a false value then it will opt out to try again! Else it will stay as a nil value.
-- If it has retrieved the inventory data then the code will continue the function call and display all of the player's assets inside the inventory!
function returnPlayerInventoryData() -- We need information from the player character's attributes.
if not player then
return
end
print(1)
local dataSort -- sorts data through a function
local getData -- retrieves the data from the server
local success, errMsg = pcall(function()
getData = inventoryDataRetrieval(player)
end)
print(2)
if not success then
if not inventoryDataReturned then
warn(errMsg, ": Inventory wasn't found!")
local loopCount = 0
local data = nil
repeat task.wait(5)
loopCount += 1
print("Searching Player: {"..player.Name.."} Inventory Data in "..loopCount.." tries!")
data = inventoryDataRetrieval(player) -- table of the data returned from the player's character by the server function
until data or loopCount == 3
if data then
inventoryDataReturned = true
for key, value in pairs(data) do
key = tonumber(data[value])
local dataSort = tableSortingData(key, value)
itemsInInventory = dataSort
end
end
loopCount = 0
inventoryDataReturned = nil
player:Kick("Player inventory was not correctly loaded! Please rejoin")
end
print("Inventory Already Exists!")
return
end
print(3)
if getData then
for key, value in pairs(getData) do
key = tonumber(getData[value])
local dataSort = tableSortingData(key, value)
itemsInInventory = dataSort
end
end
print(4)
return inventoryDataReturned
end
function inventoryDataRetrieval(player)
-- This is where a remote function call would be placed and the data returned from the server will be the inventory data!
local serverData = rf_inventoryinfo:InvokeServer()
if serverData then
return serverData
else
return nil
end
end
function tableSortingData(key, value)
itemsInInventory = {}
if key and value then
table.insert(itemsInInventory, {key, value})
table.sort(itemsInInventory, function(a, b)
return a[1] > b[1]
end)
print(itemsInInventory)
return itemsInInventory
end
return nil
end
-- MAIN
player.CharacterAppearanceLoaded:Connect(function()
print("C")
returnPlayerInventoryData()
end)
return BackpackService
Module (server script):
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemotesFolder = ReplicatedStorage:WaitForChild("Remotes")
local rf_inventoryinfo = RemotesFolder:WaitForChild("InventoryInfo")
local Inventory = {}
rf_inventoryinfo.OnServerInvoke = function(player)
if not player then
return
end
local inventory = {Items = {}}
local character = player.Character or player.CharacterAdded:Wait()
inventory["Items"]["Wood"] = character:GetAttribute("Wood")
inventory["Items"]["Stone"] = character:GetAttribute("Stone")
inventory["Items"]["Log"] = character:GetAttribute("Log")
inventory["Items"]["Torch"] = character:GetAttribute("Torch")
inventory["Items"]["Fence"] = character:GetAttribute("Fence")
return inventory
end
return Inventory