Datastore won't Save

Hello! As I was making a data saving system for my game, I found that the code that saves the data won’t work. Its gets up to the “Start Saving” print command and then fails with no errors.

Players.PlayerRemoving:Connect(function(player)
	print("Data Saving...")
	local success, errormessage = pcall(function()
		print("Starting Save") -- Last functioning print
		WinsData:SetAsync(player.UserId.."-wins", player.leaderstats.Wins.Value)
		print("wins saved")
		WinsData:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
		print("coins saved")
	end)
	
	if success then
		print("Player Data Successfully Saved!")
	else
		print("Couldn't Save!")
		warn(errormessage)
	end
end)

I have Studio access to API Services on

5 Likes

I dont get why you dont save them together, doing this will give the player two keys inside of just having one, having one key is much more efficient as you dont have to send multiple requests to get a single fragment of data, instead you can simply send one request to get all the Data.

Also, you should probably remove the print functions inside the pcall and see if that helps, just ensure that the Save was a Success and then use print to check if a specifc condition occurred.

2 Likes

I haven’t had much experience with datastore so I wasn’t sure how to save two different sets of data without having separate keys or stores

EDIT: Removing prints didn’t do anything

1 Like

I’m not sure what’s wrong with your script, but give this one a shot & let me know if it works.

Players.PlayerRemoving:Connect(function(player)
    print("Data Saving...")
    local success, errormessage = pcall(function()
        print("Starting Save")
        local winsSaved = WinsData:SetAsync(player.UserId.."-wins", player.leaderstats.Wins.Value)
        if not winsSaved then
            error("Failed to save wins for player "..player.Name)
        else
            print("wins saved")
        end
        local coinsSaved = WinsData:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
        if not coinsSaved then
            error("Failed to save coins for player "..player.Name)
        else
            print("coins saved")
        end
    end)
    
    if success then
        print("Player Data Successfully Saved!")
    else
        print("Couldn't Save!")
        warn(errormessage)
    end
end)
1 Like

This one has the same problem as the old one, it gets up to the print but doesn’t save the data

1 Like

Is API Usage allowed in game settings?

1 Like

After testing the code, I think the problem is that the game is closing before it can save so can you try to add a game:BindToClose() function with the parameter being wait to well wait before the game closes to give the datastore time to save.

Final code:

game:BindToClose(wait)

Place after the variables in your script.

Please let me know if this works.

1 Like

He claims that he has it on here.

EDIT: or is that a studio setting?

1 Like

That’s definitely not the case as PlayerRemoving will sometimes fire even when player disconnects because sometimes the player will get kicked for connection loss when you stop playtesting.

1 Like

Can you send me the complete script? @EJQuik89

1 Like

Well, I tried it in studio and it worked for me, and I also noticed that sometimes the server immediately closes due to there being no loading when you stop play testing…

1 Like

Keep in mind that the usage of BindToClose in DataStoreService is to ensure Data saves, the Studio Server is different from a Real Game Server, on Real Servers, there will be a 30 second wait for the Data and other stuff like BindToClose to properly run. On Studio there is no wait like this, so as soon as you leave, it will close immediately, BindToClose will run briefly.

1 Like

So the code will work if ran on a real server?

1 Like

It should work most of the time, but its not guaranteed to always be successful.

1 Like
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local WinsData = DataStoreService:GetDataStore("WinsData")


local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Value = 0
	Wins.Parent = leaderstats
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 0
	Coins.Parent = leaderstats
	
	local data, data2
	local success, errormessage = pcall(function()
		data = WinsData:GetAsync(player.UserId.."-wins")
		data2 = WinsData:GetAsync(player.UserId.."-coins")
	end)
	if success then
		print("Data Loaded!")
		Wins.Value = data
		Coins.Value = data2
	else
		print("Couldn't load Data!")
		warn(errormessage)
	end
end

-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)

Players.PlayerRemoving:Connect(function(player)
	print("Data Saving...")
	local success, errormessage = pcall(function()
		print("Starting Save")
		local winsSaved = WinsData:SetAsync(player.UserId.."-wins", player.leaderstats.Wins.Value)
		if not winsSaved then
			error("Failed to save wins for player "..player.Name)
		else
			print("wins saved")
		end
		local coinsSaved = WinsData:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
		if not coinsSaved then
			error("Failed to save coins for player "..player.Name)
		else
			print("coins saved")
		end
	end)

	if success then
		print("Player Data Successfully Saved!")
	else
		print("Couldn't Save!")
		warn(errormessage)
	end
end)

1 Like

This didn’t work unfortunately

Does not work at all or just sometimes?

It worked once, but then I edited it because a variable name was incorrect, and now it doesn’t at all

May I see where you pasted it?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local WinsData = DataStoreService:GetDataStore("WinsData")
game:BindToClose(wait)

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Value = 0
	Wins.Parent = leaderstats
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 0
	Coins.Parent = leaderstats
	
	local data, data2
	local success, errormessage = pcall(function()
		data = WinsData:GetAsync(player.UserId.."-wins")
		data2 = WinsData:GetAsync(player.UserId.."-coins")
	end)
	if success then
		print("Data Loaded!")
		Wins.Value = data
		Coins.Value = data2
	else
		print("Couldn't load Data!")
		warn(errormessage)
	end
end

-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)

Players.PlayerRemoving:Connect(function(player)
	print("Data Saving...")
	local success, errormessage = pcall(function()
		print("Starting Save")
		local winsSaved = WinsData:SetAsync(player.UserId.."-wins", player.leaderstats.Wins.Value)
		if not winsSaved then
			error("Failed to save wins for player "..player.Name)
		else
			print("wins saved")
		end
		local coinsSaved = WinsData:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value)
		if not coinsSaved then
			error("Failed to save coins for player "..player.Name)
		else
			print("coins saved")
		end
	end)

	if success then
		print("Player Data Successfully Saved!")
	else
		print("Couldn't Save!")
		warn(errormessage)
	end
end)