Note: I am using DataStore2 for this.
In my game, I have a section of code that is supposed to send information to the player of their inventory data, as well as print what item the player has. However, the issue is that while it works for the the first player that joins, it does not for any others that join after.
Here’s the code:
local DataStore2 = require(script.Parent.MainModule)
game.Players.PlayerAdded:Connect(function(plar)
plar.CharacterAdded:Wait()
DataStore2.Combine("Endurance1", "Coins1", "INVENTORY1")
wait(10)
local inventoryDATA= DataStore2("INVENTORY1", plar)
inventoryDATA:SetBackup(5)
local items = inventoryDATA:Get({})
if items == {} then
inventoryDATA:Set({"Standard"})
items = {"Standard"}
end
for _, individualItem in ipairs(items) do
game.ReplicatedStorage.StoreBuy:FireClient(plar, individualItem)
print(plar.Name, individualItem) -- Prints on server items player has.
end
end)
I’ve attempted various ways to attempt to solve it, such as sending down the entire table of inventory data to the client, or changing the wait times for it to process the inventory data, but nothing has worked so far.
When you are combining Keys in DataStore2, the first argument is the Master key.
Also, DataStore2.Combine does not have to be called every time a player joins.
Additionally, your script also waits for the character to be initialized and then another 10 seconds.
Here is what my script would look like:
local DataStore2 = require(script.Parent.MainModule)
DataStore2.Combine("Data", "Endurance1", "Coins1", "INVENTORY1")
game:GetService("Players").PlayerAdded:Connect(function(Player)
Player.CharacterAdded:wait()
local InventoryStore = DataStore2("INVENTORY1", Player)
InventyStore:SetBackup(5, {"Standard"})
local items = InventoryStore:Get()
for _, item in ipairs(items) do
game:GetService("ReplicatedStorage").StoreBuy:FireClient(Player, item)
print(Player.Name, item)
end
end)
While I was writing that, I saw that you had a bit of redundancy with :Get(). You can set the default value here, and then there is no need for the empty table check later on.
First off, thank you for the tips with :Get()
For the Combine part, “Endurance1” was the intended key for it. Sorry if it caused any confusion.
I’ve implemented your solution, but the issue now is that data won’t show for the first player anymore. From what I’ve deduced from running the game, having the DataStore2.Combine line in the PlayerAdded function for some reason allows for the first player to have their data.
Okay. Is there no output? If not, it might be the player scripts not loading fast enough. Instead of when the Data is ready, I would invoke a remote function on the client and then just return all of the data, once the server has it ready.