My Datastore Service doesn't save (maybe Doesn't Load) table

I’m new to datastore, but I have a problem.
I want to store tables and I wrote these scripts:

Data Saver (saves for all players)

function saveAllData()
for num,plr in ipairs(game.Players:GetPlayers()) do
	local function updateData()
		return plr.leaderstats.ObbyCoins.Value
	end
	local function updateData2()
		local items = {}
			for i,v in ipairs(plr.GameConfigs.PlayerItems:GetDescendants()) do
				items[i] = v.Value
				print(items[i])
			end 
		return items
	end
	local success,abc = pcall(function()
		coinData:UpdateAsync(plr.Name,updateData)
		itemData:UpdateAsync(plr.Name,updateData2)
	end)
	if success then
		print(plr.Name .."'s Data Updated Successfully!")
	else
		error(plr.Name .."'s Data Updated Unsuccessfully Because Error: ".. abc)
	end
end
end

Data Creator

function manageData(player)

	local done,amount = pcall(function()
		itemData:GetAsync(player.Name)
	end)										
	local done2,amount2 = pcall(function()
		itemData:GetAsync(player.Name)
	end)
	-------------------------------------------

	if not done then
		print("Creating Coin Data...")
		local done,err = pcall(function()
			coinData:SetAsync(player.Name,0)
		end)
		if done then
			print("Data Created For:".. player.Name)
		else
			error("Process Failed Because Error "..err)
		end
	end
	------------------------------------------
	if not done2 then
		print("Creating Item Data...")
		local done,err = pcall(function()
			itemData:SetAsync(player.Name,{})
		end)
		if done then
			print("Data Created For:".. player.Name)
		else
			error("Process Failed Because Error "..err)
		end
	end
	------------------------------------------

	local done,amount = pcall(function()
		player.leaderstats.ObbyCoins.Value = coinData:GetAsync(player.Name)
	end)

	local done,amount = pcall(function()
	local itemTable = itemData:GetAsync(player.Name)
	print(unpack(itemTable))
		for i,v in ipairs(itemTable) do
			if v == "RedTrail" then
				BIH.createTrail("RedTrail",ColorSequence.new(Color3.fromRGB(0,0,0),Color3.fromRGB(255,0,0)),player,true)
			end
		end
	end)

	

end
Basic Item Handler (or BIH, creates basic items like trails.)
local BItemHandler = {}

function BItemHandler.createTrail(name,color,player,boughtfirsttime)
local item = Instance.new("Trail")
item.Parent = player.character.Head
item.Color = color
item.Name = name
item.Enabled = true
item.Lifetime = 2

local att0 = Instance.new("Attachment",player.character.HumanoidRootPart)
att0.Name = "TrailAtt0"
att0.Position = Vector3.new(0,1,0)

local att1 = Instance.new("Attachment",player.character.HumanoidRootPart)
att1.Name = "TrailAtt1"
att1.Position = Vector3.new(0,-1,0)

item.Attachment0 = att0
item.Attachment1 = att1
if boughtfirsttime then
	local itemOwn = Instance.new("StringValue",player.GameConfigs.PlayerItems)
	itemOwn.Name = "itemTrail"
	itemOwn.Value = name
end

end

return BItemHandler

But, It doesn’t saves or loads any data that I want.

What is the problem with these scripts?

There’s… enough wrong with the script, try this tutorial by AlvinBlox, it’ll help you out: https://www.youtube.com/watch?v=DkYupSBUpes

One issue here is you are most likely testing it in studio, and you’ll need a bind to close function, since studio shuts down pretty fast

Ok, but I binded save function before like this:

game:BindToClose(coroutine.wrap(saveAllData))

I would actually test this in an actual game environment instead of in studio, because datastores break in studio (They save/don’t save randomly)

What was this post? Why was it deleted? Why was it given solution?

DataStores work exactly like they’re supposed for me in studio. If they arent saving sometimes it’s because your code is flawed!

This is not a solution (since many have already provided information above) but you should keep it mind to never save data to the player’s name. Use their ID, since the player’s name can be changed. You can use player.UserId or game.Players:GetNameFromUserIdAsync(player) depending on your situation. If you need to retrieve data while they are offline, the second option would work better.

That will not work since you never called the coroutine.

Anyway I fixed it. I rewritten code like this:
Data Creator

function manageData(player)

local done,amount = pcall(function()
	itemData:GetAsync(tostring(player.UserId))
end)										
local done2,amount2 = pcall(function()
	itemData:GetAsync(tostring(player.UserId))
end)
-------------------------------------------

if not done then
	print("Creating Coin Data...")
	local done,err = pcall(function()
		coinData:SetAsync(tostring(player.UserId),0)
	end)
	if done then
		print("Data Created For:".. tostring(player.UserId))
	else
		error("Process Failed Because Error "..err)
	end
end
------------------------------------------
if not done2 then
	print("Creating Item Data...")
	local done,err = pcall(function()
		itemData:SetAsync(tostring(player.UserId),{})
	end)
	if done then
		print("Data Created For:".. tostring(player.UserId))
	else
		error("Process Failed Because Error "..err)
	end
end
------------------------------------------

local done,amount = pcall(function()
	player.leaderstats.ObbyCoins.Value = coinData:GetAsync(tostring(player.UserId))
end)

local done,amount = pcall(function()
local itemTable = itemData:GetAsync(tostring(player.UserId))
print(unpack(itemTable))
	for i,v in ipairs(itemTable) do
		if v == 1 then
			BIH.createTrail(1,ColorSequence.new(Color3.fromRGB(0,0,0),Color3.fromRGB(255,0,0)),player,true)
		end
		if v == 2 then
			BIH.createTrail(2,ColorSequence.new(Color3.fromRGB(0,0,0),Color3.fromRGB(255,0,0)),player,true)
		end
	end
end)

Data Saver

function saveAllData()
for num,plr in ipairs(game.Players:GetPlayers()) do
local function updateData()
	return plr.leaderstats.ObbyCoins.Value
end
local function updateData2()
	local items = {}
		for i,v in ipairs(plr.GameConfigs.PlayerItems:GetChildren()) do
			items[i] = v.Value
			print(items[i],v.Name)
		end 
	return items
end
local success,abc = pcall(function()
	coinData:UpdateAsync(tostring(plr.UserId),updateData)
	itemData:UpdateAsync(tostring(plr.UserId),updateData2)
end)
if success then
	print(plr.Name .."'s Data Updated Successfully!")
else
	error(plr.Name .."'s Data Updated Unsuccessfully Because Error: ".. abc)
end
end
end

I changed key value player.Name to tostring(player.UserId) to save data.
But also the value I changed into ModuleScript is applied locally, since require() copies ModuleScript, so I removed these lines:

if boughtfirsttime then
local itemOwn = Instance.new("StringValue",player.GameConfigs.PlayerItems)
itemOwn.Name = "itemTrail"
itemOwn.Value = name

end

And I have gui to buy trails, I changed buy button script like this:

local BItemHandler = require(game.ReplicatedStorage.GameItems.BasicItemHandler)
script.Parent.MouseButton1Click:Connect(function()
local plr = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent
if plr.leaderstats.ObbyCoins.Value > -1 then
	plr.GameConfigs.PlayerItems.Trail.Value = 2
	 BItemHandler.createTrail(2,ColorSequence.new(Color3.fromRGB(0,0,0),Color3.fromRGB(0,255,0)),plr)
end
end)

Yeah, “script.Parent.Parent.Parent.Parent.Parent.Parent.Parent” line is weird,because this code inside of normal script, because local script applies values locally, not globally.

Anyway, thanks for your help!