Datastore script isn't saving but there isn't errors

  1. What do you want to achieve?

I want to be able to save data in my game.

  1. What is the issue?

My datastore script isn’t saving the data.

  1. What solutions have you tried so far?

I have tried debugging and I found out that the thing that is not working is is the :setasync part

datastore script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")

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
	
	print("finished creating leaderstats")
	
	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(player.UserId)
	end)
	
	print("finish loading data")
	
	if success then
		cash.Value = data
	end
	
	print("done with player added script")
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local data = {player.leaderstats.Cash.Value}
	
	print("defined the data")
	
	local success, errormessage = pcall(function()
		DataStore:SetAsync(player.UserId, data )--part where it goes wrong
	end)
	
	print("saved the data ")
	
	if success then
		print("we saved the data")
	else
		print("there is a error")
		warn(errormessage)
	end
end)

1 Like

Roblox Studio closes the whole game and the playeradded doesn’t have time to run. Use game:BindToClose() or just test in the actual game.

game:BindToClose(function()
    --save here (for every player)
end)
2 Likes

However now I got a new problem in which the error says

 DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 1503729675 

I know this means that there is too much requests but I don’t know how to fix it

new script

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")

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
	
	print("finished creating leaderstats")
	
	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(player.UserId)
	end)
	
	print("finish loading data")
	
	if success then
		cash.Value = data
	end
	
	print("done with player added script")
end)

local data

game.Players.PlayerRemoving:Connect(function(player)
	
	data = {player.leaderstats.Cash.Value}
	
	print("defined the data")
	
	local success, errormessage = pcall(function()
		DataStore:SetAsync(player.UserId, data)
	end)
	
	print("saved the data ")
	
	if success then
		print("we saved it")
	else
		print("there was a error")
		warn(errormessage)
	end
end)

game:BindToClose(function()
	local success, errormessage = pcall(function()
		for i, player in pairs(game.Players:GetPlayers()) do
			DataStore:SetAsync(player.UserId, data)
		end
	end)

	print("saved the data ")

	if success then
		print("we saved it")
	else
		warn(errormessage)
	end
end)

this isn’t an error. It just says that whenever you save data as to remind you not to constantly save data.

however when I close the game roblox studio for a solid coupe seconds, is that normal?

Yes, as the game needs time to save the data.