Hello,
My Datastore is not saving the items within the folder I've set up accurately. My game is set up in rounds, player purchases items and they stay in a backpack, round happens/ends, rinse and repeat, no problem. When a player leaves the game and returns, the items are still in the backpack, however, after the first round ends, the items have disappeared. How do I prevent the items from disappearing permanently?
The script below was created with much help from the wonderful devs here, and I'm not quite sure where I've gone wrong below. Thank you for your help!
local ds = game:GetService("DataStoreService"):GetDataStore("Tools");
local tools = game.ServerStorage.ToolStorage; --Folder created to store Tools.
--{[Check and Load]}
game.Players.PlayerAdded:connect(function(plr)
--Retrieve the potential data
local data = ds:GetAsync(plr.UserId);
--Check if it exists
if data then
--If it does, loop and clone respective tools.
for _,v in next,data do
local tool = tools:FindFirstChild(v);
if tool then
tool:Clone().Parent = plr.Backpack;
end
end
end
end)
--{[Collect and Save]}
game.Players.PlayerRemoving:connect(function(plr)
--Make a table to hold all the data
local toolList = {};
--Iterate through the backpack and fill the table in
for _,v in next,plr.Backpack:GetChildren() do
--Make sure it's actually a tool
if v:IsA("Tool") or v:IsA("HopperBin") then
toolList[#toolList+1] = v.Name;
end
end
--Save the table
ds:SetAsync(plr.UserId,toolList);
end)

3 comments
:D It seems to work! Got a strange issue now to where the items don't load when a player enter, but AFTER the first round they do. Could I add a Wait(3) somewhere to fix that?
Nevermind, its not saving them after all. :(
Oh, sorry. I wish I knew datastores.