So I am currently making an Obby. I have a shop GUI in which people can buy items if they have enough money. P.S: This is a YT tutorial. I have placed a script inside serverscriptservice which is as follows:
local ds = game:GetService("DataStoreService"):GetDataStore("ToolSavee")
game.Players.PlayerAdded:connect(function(plr)
local key = "id-"..plr.userId
pcall(function()
local tools = ds:GetAsync(key)
if tools then
for i,v in pairs(tools) do
local tool = game.ServerStorage.Tools:FindFirstChild(v)
if tool then
tool:Clone().Parent = plr:FindFirstChild("Backpack")
tool:Clone().Parent = plr:FindFirstChild("StarterGear")
end
end
end
end)
end)
game.Players.PlayerRemoving:connect(function(plr)
local key = "id-"..plr.userId
local toolsToSave = {}
for i,v in pairs(plr.Backpack:GetChildren()) do
if v then
table.insert(toolsToSave,v.Name)
end
end
ds:SetAsync(key,toolsToSave)
end)
But the problem is that this script is only saving the first item which the player buys e.g. If there are 2 items in the shop, and the player has enough money to buy both of them, so the first item he will buy will be stored in the backpack and if he buys the second item, the second item will be given to him.
But when the player rejoins the game, only the item he bought first will be saved not the other item.
What is wrong in this script?
Please do help.
Thanks

4 comments
Thanks a lot!
No problem.
Oh but I just figured out that if the player buys another item it is not saved even though the first 2 are saved. Any idea?
If the tool is equipped it won't save. But if you want the tool to save even though it is equipped then try looking at @TheMinecraftSmart's answer.