DataStore only saves old things

I have a datastore system where it saves the tools in the player’s storage by storing the name of the values and then the amount. Then when the player loads in it grabs the items from server storage and puts it in their storage. It worked great. Recently when I started adding items to the server storage I found that it would only save the “OG” items in the game. I thought it could be from the new items I was implementing but to test this I duplicated an “OG” item and renamed it. It wouldn’t save. This means the datastore broke and only saves what it remembers was in the folder before? Any fix to this? Code below.

local DSS = game:GetService(“DataStoreService”)
local DS = DSS:GetDataStore(“Storage”)
local All = game.ServerStorage:WaitForChild(“Trade”)
game.Players.PlayerAdded:Connect(function(Player)
local Inventory = Instance.new(“Folder”,Player)
Inventory.Name = “Storage”
local Data
local Success,err = pcall(function()
Data = DS:GetAsync(Player.UserId)
end)
if not Success then
Player:Kick("Error loading data: "…err)
else
if not Data then
Data = {}
end
for index,value in pairs(Data) do
if All:FindFirstChild(value) then
All:FindFirstChild(value):Clone().Parent = Inventory
end
end
end

end)

game.Players.PlayerRemoving:Connect(function(Player)
if Player:FindFirstChild(“Storage”) then
local t = {}
for index,value in pairs(Player:FindFirstChild(“Storage”):GetChildren()) do
if game.ServerStorage.Trade:FindFirstChild(value.Name) then
t[#t+1] = value.Name
end
end
DS:SetAsync(Player.UserId,t)
else
warn("Storage missing in player " … Player.Name)
end
end)

game:BindToClose(function()
task.wait(3)
end)

3 Likes

Merry Christmas!
Before checking exactly what happened to you, please read and check the following carefully:

  1. DataStore Key: If you’ve recently changed the DataStore key, you might encounter issues with saving new data. Make sure you’re using the same key to save and load data.
  2. Data Verification: Ensure the data you’re trying to save is valid. For example, if you’re trying to save an object that doesn’t exist or has been deleted, it won’t be saved.
  3. Data Errors: There might be a data error. Try printing your entire DataStore to see if it still holds your data.

However, you mentioned that only the “OG” (original) items are being saved and new items are not being saved.

  • One possible reason for this issue is that the DataStore is not being updated correctly when new items are added to the server storage. In your code, you are only saving the names of the items in the player’s inventory, not the actual instances of the items. Therefore, when you try to load the items from the server storage, it only finds the original items that were present when the DataStore was last updated.

I also prepared a code, hoping it could help you, this is the code hoping it will work, not knowing exactly if the 3 requirements above are checked and done as I said, I can’t clearly give you the correct code 100%. Who knows, maybe it will work!

local DataStoreService = game:GetService("DataStoreService")
local DS = DataStoreService:GetDataStore("Storage")
local All = game.ServerStorage:WaitForChild("Trade")

game.Players.PlayerAdded:Connect(function(Player)
    local Inventory = Instance.new("Folder")
    Inventory.Name = "Storage"
    Inventory.Parent = Player

    local Data
    local Success, err = pcall(function()
        Data = DS:GetAsync(Player.UserId)
    end)

    if not Success then
        Player:Kick("Error loading data: " .. err)
    else
        if not Data then
            Data = {}
        end

        for index, value in pairs(Data) do
            local item = All:FindFirstChild(value)
            if item then
                item:Clone().Parent = Inventory
            end
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local Inventory = Player:FindFirstChild("Storage")
    if Inventory then
        local t = {}
        for index, value in pairs(Inventory:GetChildren()) do
            if game.ServerStorage.Trade:FindFirstChild(value.Name) then
                t[#t + 1] = value.Name
            end
        end
        DS:SetAsync(Player.UserId, t)
    else
        warn("Storage missing in player " .. Player.Name)
    end
end)

game:BindToClose(function()
    task.wait(3)
end)

As a warning, I recommend that when you make a leaderboard, when you create the folder, don’t use directly

 local Inventory = Instance.new("Folder", Player)

It is not so safe, and as far as I know, it is also recommended by Roblox not to do such a thing like that.

It’s best like this:

local Inventory = Instance.new("Folder")
    Inventory.Name = "Storage"
    Inventory.Parent = Player

Also next time, please put the code in a code block, because it’s very hard to understand the code if you don’t put the code in a code block. :smile:

For more info about DataStoresclick here, and here

I tried the code you sent. It still only saves the OG items, also about the code block part I don’t have the squiggle on my keyboard and the forum normally automatically puts it in for me. (this time it did not)

Oh:(
I really don’t have any ideeas why it doesn’t work…

I am going to try to conduct this on an empty place to see if the problem is other scripts interfering with it

It appears to work completely fine on a new baseplate, including both new and old items. Do you have any idea what kind of script could be interfering with this process?

it may not be working because u are not deleting the player data folder from the game
try this :

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Storage")
local All = game.ServerStorage:WaitForChild("Trade")
game.Players.PlayerAdded:Connect(function(Player)
	local Inventory = Instance.new("Folder",Player)
	Inventory.Name = "Storage"
	local Data
	local Success,err = pcall(function()
		Data = DS:GetAsync(Player.UserId)
	end)
	if not Success then
		Player:Kick("Error loading data: "..err)
	else
		if not Data then
			Data = {}
		end
		for index, value in pairs(Data) do
			if All:FindFirstChild(value) then
				All:FindFirstChild(value):Clone().Parent = Inventory
			end
		end
	end

end)


game.Players.PlayerRemoving:Connect(function(Player)
	if Player:FindFirstChild("Storage") then
		local t = {}
		local _count = 0
		for index, value in pairs(Player:FindFirstChild("Storage"):GetChildren()) do
			if game.ServerStorage.Trade:FindFirstChild(value.Name) then
				table.insert(t, value.Name)
				_count += 1
			end
		end
		print(_count)
		if table.maxn(t) == _count then
			Player:FindFirstChild("Storage"):Destroy()
		end
		DS:SetAsync(Player.UserId,t)
	else
		warn("Storage missing in player " .. Player.Name)
	end
end)

game:BindToClose(function()
	task.wait(3)
end)

if it doesn’t work let me know I might have another solution

once again did not work, what’s the alternate solution?

run this code and let me see output

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Storage")
local All = game.ServerStorage:WaitForChild("Trade")
game.Players.PlayerAdded:Connect(function(Player)
	local Inventory = Instance.new("Folder",Player)
	Inventory.Name = "Storage"
	local Data
	local Success,err = pcall(function()
		Data = DS:GetAsync(Player.UserId)
	end)
	if not Success then
		Player:Kick("Error loading data: "..err)
	else
		if not Data then
			Data = {}
		end
		for index, value in pairs(Data) do
                        print(value)
			if All:FindFirstChild(value) then
				All:FindFirstChild(value):Clone().Parent = Inventory
			end
		end
	end

end)


game.Players.PlayerRemoving:Connect(function(Player)
	if Player:FindFirstChild("Storage") then
		local t = {}
		local _count = 0
		for index, value in pairs(Player:FindFirstChild("Storage"):GetChildren()) do
			if game.ServerStorage.Trade:FindFirstChild(value.Name) then
				table.insert(t, value.Name)
				_count += 1
			end
		end
		print(_count)
		if table.maxn(t) == _count then
			Player:FindFirstChild("Storage"):Destroy()
		end
		DS:SetAsync(Player.UserId,t)
                print(table.unpack(t))
	else
		warn("Storage missing in player " .. Player.Name)
	end
end)

game:BindToClose(function()
	task.wait(3)
end)

when leaving it printed all the correct items the first couple of times in studio, when tested in-game printed only og items. the problem must be from a different script interfering with the new items.

so u are saying it worked in studio but not in game? can u please show me the output

in studio (this is all the items)

02:47:50.418 Basic Chest - Server - StorageSave:18
02:47:50.419 Clone Scroll - Server - StorageSave:18
02:47:50.419 Complex Chest - Server - StorageSave:18
02:47:50.420 Earth Scroll - Server - StorageSave:18
02:47:50.420 Fire Scroll - Server - StorageSave:18
02:47:50.421 Hidan Part - Server - StorageSave:18
02:47:50.421 Kusanagi Part - Server - StorageSave:18
02:47:50.421 Obito Mangekyou - Server - StorageSave:18
02:47:50.421 Race Reroll - Server - StorageSave:18
02:47:50.422 SP Reset - Server - StorageSave:18
02:47:50.422 Sharingan - Server - StorageSave:18
02:47:50.422 Slot Unlocker - Server - StorageSave:18
02:47:50.423 Test Scroll - Server - StorageSave:18
02:47:50.423 Unique Chest - Server - StorageSave:18
02:47:50.424 Water Scroll - Server - StorageSave:18
02:47:50.424 Wind Scroll - Server - StorageSave:18
02:47:50.425 Wood Scroll - Server - StorageSave:18
02:47:50.425 Zabuza Part - Server - StorageSave:18

in-game (this is not all the items)

image

try this

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Storage")
local All = game.ServerStorage:WaitForChild("Trade")
local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(Player)
	local Inventory = Instance.new("Folder",Player)
	Inventory.Name = "Storage"
	local Data
	local Success,err = pcall(function()
		Data = DS:GetAsync(Player.UserId)
	end)
	if not Success then
		Player:Kick("Error loading data: "..err)
	else
		if not Data then
			Data = {}
		end
		for index, value in pairs(Data) do
			print(value)
			if All:FindFirstChild(value) then
				All:FindFirstChild(value):Clone().Parent = Inventory
			end
		end
	end

end)


local function Save(Player : Player, Destroy : boolean)
	if Player:FindFirstChild("Storage") then
		local t = {}
		local _count = 0
		for index, value in pairs(Player:FindFirstChild("Storage"):GetChildren()) do
			if game.ServerStorage.Trade:FindFirstChild(value.Name) then
				table.insert(t, value.Name)
				_count += 1
			end
		end
		print(_count)
		if Destroy then
			Player:FindFirstChild("Storage"):Destroy()
		end
		DS:SetAsync(Player.UserId, t)
		print(table.unpack(t))
	else
		warn("Storage missing in player " .. Player.Name)
	end
end

local function SaveAllData()
	for _, plr in pairs(Players:GetPlayers()) do
		coroutine.wrap(function()
			Save(plr, false)
		end)()
	end
end

game.Players.PlayerRemoving:Connect(function(Player)
	Save(Player, true)
end)

game:BindToClose(function()
	SaveAllData()
end)

I just realized that this issue was due to the game taking place in a separate place from the initial place, but both shared the same datastore. The initial starting place did not have the new items. This caused the problem by not saving it in the first place. I am so sorry for wasting your time.

1 Like

Its ok :+1: I recommend you use the script I sent u its more secure but even better is ProfileService you should use it if you are making a new game, although you shouldn’t feel obliged if the game is not complex

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.