I have a system where a player is given an inventory when they join the game. Then, a local script that gets this inventory from the server using a remote event. However, sometimes when I test the game, the inventory is nil and nothing is transferred to the player - other times it is not nil and the inventory data is transferred. I don't really know how to fix this.
This is the function to get a player's inventory (the inventory is stored on the server)
function RetrieveInv(player)
wait(InventoryModule:GetInventory(player):GetContents()) -- this should (intheory) wait for the inventory to exist
Event:FireClient(player, InventoryModule:GetInventory(player):GetContents()) -- send inventory to client
end
Sometimes, the fireclient code runs, and fails because it is nil, other times it runs but an empty table is sent to the client. A lot of the time everything works though.
This is the local script that prints out the inventory
local invData
function setupInv()
Event:FireServer()
wait(invData)
print(Player.Name .. "'s Items:")
if invData ~= nil then
for i, item in pairs(invData)do
print("Name:" .. i .. "\nAmount: " .. item.Amount .. "\nType: " .. item.Content.ItemClass)
end
end
end
function updateInv(newInv)
invData = newInv
end
Event.OnClientEvent:Connect(updateInv)
setupInv()
I know it's to do with concurrency - but I don't know how to force the game to wait for the inventory to exist before it sends anything to the client.
Adding an if statement to check if it is not nil works, but sometimes nothing is sent over, the local script stops trying to get data and the output is empty.

4 comments
Okay I'll try this. And no it's fine, the inventory is completely stored on the server, a datastore actually. Forgot to mention
Would the BoolValue be on the server then? Would that mean it uses the same BoolValue for multiple players?
alibix123, you can create a table for each player when a player joins and set the Player object as the index and a boolean value as the value to false. Once data loads on the server set the value to true. Yes it is on the server and no you wouldn't use the same BoolValue for multiple players
Also if your using a BoolValue object then I suggest you make one and put it into the StarterCharacterScripts since this way when a player spawns they all get an object