Data store not saving

okay so I’m scripting this data store and I’m really confused because I’ve never experienced this type of error before

function SaveData(Donated, Raised, Robux, Player)
	print("Saving data.")
	
	local Data = {}

	Data.Donated = Donated
	Data.Raised = Raised
	Data.Robux = Robux

	-- does anything before here

	MainDataStore:SetAsync(Player.UserId, Data) -- fails here with no error
	
	print("Saved data.")
end

so it does anything before setasync but fails on the same line. I am so confused and any help would be awesome, thanks.

If it is not giving any error, can you try publishing and testing the game in Roblox?

You are probably testing in studio, use this:

game:BindToClose(function()
	for i,player in pairs(game.Players:GetChildren())do
		SaveData(donated,raised,robux,player)
	end
end)

bindtoclose won’t fail to work in studio, this is how I fixed it when I faced the same issues. (This is additional to when server crashes, or game shutdowns, you must always have the PlayerRemoving event too, cause only then it will save players data when they leave game from a multi player server)

2 Likes

this was the solution, thank you so much.

You wouldn’t do that. Because if a player leaves the game then his data would not be saved.
You have to save data when player leaves.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("DataStoreName")

Players.PlayerRemoving:Connect(function(Player)
   local Leaderstats = Player:FindFirstChild("leaderstats")
   local CashValue = Leaderstats:FindFirstChild("Cash")

   if RunService:IsStudio() then return end --This line will prevent the studio from continuing

   local Success, Response = pcall(function()
      DataStore:SetAsync(Player.UserId, CashValue.Value)
   end)

   if not Success then
      warn("Error occured during DataStore saving: " .. Response)
   end
end)

Also don’t forget that this is a server-side script.

1 Like

oh, I didn’t know you had to do that. thank you so much

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.