Datastore script does not do anything

So I want to save my items but it just does not work for some reason, it worked before but now it does not work anymore.

If any value gets changed on server side and I disconect and go in again nothing happened it just remained the same as before.

It also does not give any warnings or errors, it also prints “Found data” from the line 107.

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local items = replicatedStorage.Items

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("InventoryData")


local tries = 3
local dataloaded = nil

local function serialize(plr)
	if dataloaded then
		local key = plr.UserId
		local count = 0

		local data = {}

		for i, v in ipairs(plr.Items:GetChildren()) do
			if v and v:IsA("NumberValue") then
				data[v.Name] = v.Value
			end
			
		for i, v in ipairs(plr.Items.Ores:GetChildren()) do
		if v and v:IsA("NumberValue") then
				data[v.Name] = v.Value
				end
				
		for i, v in ipairs(plr.Items.ItemLevels:GetChildren()) do
			if v and v:IsA("NumberValue") then
				data[v.Name] = v.Value
						
			end
				end
				end
end
		local success, err

		repeat
			success, err = pcall(function()
				wait(1)
				dataStore:SetAsync(key, data)
			end)

			count = count + 1
		until count >= tries or success

		if not success then
			warn("Data could not be set." .. tostring(err))

			return
		end
	else
		warn("Data has not been loaded. Do not attempt to set data when it has not been loaded.")

		return
	end
end
local function deserialize(plr)
	local key = plr.UserId
	local count = 0

	local data

	local success, err

	repeat
		success, err = pcall(function()
			wait(1)
			data = dataStore:GetAsync(key)
		end)

		count = count + 1
	until count >= tries or success

	if not success then
		warn("Failed to read data." .. tostring(err))

		plr:Kick("Failed to read data. Please rejoin the game.")

		return
	end

	if data then
		dataloaded = true

		return data
	else
		dataloaded = true

	end
end

local function createInventory(plr)
	local dataFolder = Instance.new("Folder", plr)
	dataFolder.Name = "Items"

	local item = Instance.new('Folder', dataFolder)
	item.Name = 'Ores'
	
	local itemlevels = Instance.new('Folder', dataFolder)
	itemlevels.Name = 'ItemLevels'

	local data = deserialize(plr)

	if data then
		print("Found data.")
		for i, v in ipairs(items:GetChildren()) do
			local int = Instance.new('NumberValue', dataFolder)
			int.Name = v.Name
			int.Value = data[v.Name]

			end	
		for i, v in ipairs(replicatedStorage.Ores:GetChildren()) do	
				local int = Instance.new('NumberValue', item)
				int.Name = v.Name
			int.Value = data[v.Name]
			end
		for i, v in ipairs(replicatedStorage.ItemLevels:GetChildren()) do	
				local int = Instance.new('NumberValue', itemlevels)
				int.Name = v.Name
			int.Value = data[v.Name]
			end
	else
		
		print("No data found.")
		for i, v in ipairs(items:GetChildren()) do
			local int = Instance.new('NumberValue', dataFolder)
			int.Name = v.Name

		end	
		for i, v in ipairs(replicatedStorage.Ores:GetChildren()) do	
			local int = Instance.new('NumberValue', item)
			int.Name = v.Name
		end
		for i, v in ipairs(replicatedStorage.ItemLevels:GetChildren()) do	
			local int = Instance.new('NumberValue', itemlevels)
			int.Name = v.Name
		end	
		plr.Items:WaitForChild("BagCost").Value = 50
		plr.Items:WaitForChild("maxBag").Value = 50
		plr.Items:WaitForChild("BagLevel").Value = 1
		plr.Items.ItemLevels:WaitForChild("PickaxeDmgLVL").Value = 1
		plr.Items.ItemLevels:WaitForChild("PickaxeLevel").Value = 1
		plr.Items.ItemLevels:WaitForChild("PickaxeSpeed").Value = 1
		plr.Items:WaitForChild("Stage").Value = 1
		plr.Items:WaitForChild("CurrentOre").Value = 1
		plr.Items:WaitForChild("SpeedCost").Value = 50
		end    
end


players.PlayerAdded:Connect(createInventory)
players.PlayerRemoving:Connect(serialize)

game:BindToClose(function()
	for i, plr in ipairs(players:GetPlayers()) do
		wait(10)
		serialize(plr)
	end
end)
1 Like

anyone? it still does not work for some reason

Does it print anything else other than , “Found Data”?

It never did and it still does not, when it worked it didn’t print anything else.

Your game datastores might have gone wrong, try check the game datastores. If they’re losing data, try replicate everything into another baseplate.