Data Store Won't Save

Hello!
I am currently testing out some data saving things and I made this script, however the data will not save…
Here is the script:
local dataStoreService = game:GetService(“DataStoreService”)
local MyDataStore = dataStoreService:GetDataStore(“MyDataStore”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
cash.Value = 0

local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = 0

local PlayerUserId = "Player_"..player.UserId

local success, errormessage = pcall(function()
	data = MyDataStore:GetAsync(PlayerUserId)
	print(data)
end)

if success then 
	--set current values to data 
	print(data.Cash)
	cash.Value = data.cash
	coins.Value = data.coins
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local PlayerUserId = “Player_”…player.UserId

local data = {
	cash = player.leaderstats.Cash.Value;
	coins = player.leaderstats.Coins.Value;
}

local success,errormessage = pcall(function()
	MyDataStore:SetAsync(PlayerUserId,data)
end)

print("Saving has been finished")
if success then 
	print("Data successfully saved")
else
	warn(errormessage)
end

end)

And there is a spot that says “Player_”…player.UserId however this is just a typo on the dev forum, I have it written with two dots.
It won’t even print out “Saving has been finished”… How can I fix this? Thanks! I’m not very good with DataStores.

You forgot a BTC (BindToClose) this is helpful for when the server shuts down it doesn’t read PlayerRemoving when the server closes.

game:BindToClose(function()

end)

So trade that out for the player removing?

No, keep PlayerRemoving but add a BindToClose aswell.

Have you Enabled studio acess to API Services?

Otherwise “Player_”…player.UserId should be

“Player_”…player.UserId in the Saving function

There is only supposed to be two dots it must be bugging out.

Okay I’ll try that thanks. And I did enable studio access to API Services

I wrapped everything in the player removing inside of a BTC but it still won’t save…

Can you send your updated code?

I got this error: BindToClose failed because the model is already being closed.

local dataStoreService = game:GetService(“DataStoreService”)
local MyDataStore = dataStoreService:GetDataStore(“MyDataStore”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
cash.Value = 0

local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = 0

local PlayerUserId = "Player_"..player.UserId

local success, errormessage = pcall(function()
	data = MyDataStore:GetAsync(PlayerUserId)
	print(data)
end)

if success then 
	--set current values to data 
	print(data.Cash)
	cash.Value = data.cash
	coins.Value = data.coins
end

end)

game.Players.PlayerRemoving:Connect(function(player)
game:BindToClose(function()
local PlayerUserId = “Player_”…player.UserId

	local data = {
		cash = player.leaderstats.Cash.Value;
		coins = player.leaderstats.Coins.Value;
	}
	
	local success,errormessage = pcall(function()
		MyDataStore:SetAsync(PlayerUserId,data)
	end)
	
	print("we did the saving")
	if success then 
		print("Data successfully saved")
	else
		warn(errormessage)
	end
end)

end)

I forgot to mention you need to do a for loop and get all the players in the server for the BTC and save it that way.

Okay, I’ll try my best to make that.

is this better?
game.Players.PlayerRemoving:Connect(function(player)
game:BindToClose(function()

	for i,v in pairs(game.Players:GetPlayers()) do
		local PlayerUserId = "Player_"..v.UserId
		
		
		local data = {
			cash = v.leaderstats.Cash.Value;
			coins = v.leaderstats.Coins.Value;
		}
		
		local success,errormessage = pcall(function()
			MyDataStore:SetAsync(PlayerUserId,data)
		end)
		
		print("we did the saving")
		if success then 
			print("Data successfully saved")
		else
			warn(errormessage)
		end
	end
end)

end)

1 Like

Okay, it worked it thank you!!!