Datastore not working - errors after Data:SetAsync

I really didn’t want to post here since it just felt like an easy fix but I’m at my wits end. Any help is much appreciated.

local ServerStorage = game:GetService("ServerStorage")
local PlayerService = game:GetService("Players")

local Datastore = require(script.Datastore)

_G.inTestingMode = true

PlayerService.PlayerAdded:Connect(Datastore.Load)
PlayerService.PlayerRemoving:Connect(Datastore.Save)
game:BindToClose(Datastore.ForceSave)
local Data = {}

local PlayerService = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")
local Datastore = DatastoreService:GetDataStore("Statistics")

function Data.Load(Player)
	
	local Data
	local leaderstats
	local PlayerID = "Player_" .. Player.UserId
	
	-- Load folder
	for i, v in ipairs(script.Parent.PlayerData:GetChildren()) do
		v:Clone().Parent = Player
	end
	
	-- Collect data
	local Success, ErrorMessage = pcall(function()
		Data = Datastore:GetAsync(PlayerID)
	end)
	
	-- Load data
	if Success and Data then
		for i, v in ipairs(Player:FindFirstChildWhichIsA("Folder"):GetChildren()) do
			v.Value = Data[v.Name]
		end
	end
	
	local ServerStorage = game:GetService("ServerStorage")
	local PlayerStates = ServerStorage.PlayerStates
	local SubjectState = require(script.Parent.SubjectState)
	
	-- Load player states
	local StatesTemplate = script.Parent.PlayerStatesTemplate:Clone()
	StatesTemplate.Name = Player.Name
	StatesTemplate.Parent = PlayerStates
	
	-- State interaction
	for i, v in ipairs(PlayerStates:GetChildren()) do
		v.Changed:Connect(function(Value)
			SubjectState.Activate(Player, v.Name, Value)
		end)
	end
	
end

function Data.Save(Player)
	
	-- Variables
	local PlayerID = "Player_" .. Player.UserId
	local Data = {}

	-- Collect data
	for i, v in ipairs(Player:FindFirstChildWhichIsA("Folder"):GetChildren()) do
		Data[v.Name] = v.Value
	end
	
	 -- Save data
	local Success, ErrorMessage = pcall(function()
		Datastore:SetAsync(PlayerID, Data)
	end)
	
	if not Success then
		warn("Error: Data not saved.")
	else
		print("Data saved successfully.")
	end
	
end

function Data.ForceSave()

	for i, Player in ipairs(PlayerService:GetPlayers()) do
		task.spawn(function()
			Data.Save(Player)
		end)
	end
	
end

return Data

1 Like

Specify in detail what the errors are.

What’s the values your setting, is it a number, boolean, or a string?

Alright, so I can suggest you my DataStore solution checklist I have:

  1. Make sure the Datastore service is enabled in your game settings.
  2. Check if the PlayerData folder exists in the script.Parent and it has the necessary data.
  3. Ensure the PlayerStates folder exists in ServerStorage.
  4. Make sure the SubjectState module exists and has an Activate function.
  5. Check if the PlayerStatesTemplate exists in script.Parent.
  6. Ensure that the data you’re trying to save is serializable. Roblox’s DataStore service can only save basic Lua types (string, number, boolean, table, etc.).
  7. Use print or warn to output ErrorMessage if the pcall fails. This can give you more information about what’s going wrong.
1 Like

No errors show up. “Data saved” and “Data not saved” do not print either. Any prints past Datastore:SetAsync() do not print.

image
It is only Integer Values for now, but would hope to expand to other types.

1 Like
  1. Checked
  2. Yep
  3. Mhm
  4. Yes
  5. Yeah
  6. Indeed
  7. Nothing prints when i check for ErrorMessage

Most of these checks would break the entire script and would be highlighted so it would be an easy fix, however code above SetAsync only works and not below it.

Is the function getting fired?

Yes. As I said before prints did respond to the function so I have already checked for that, just not after SetAsync().

Are you testing this on studio? Enable API services and instead of stopping the game, leave the game via the Roblox menu.

Bingo. I was looking at a comment on a previous Datastore tutorial and that’s what it said. That being said, I did include a BindToClose function which was supposed to save data when the server shuts down before the player can save data on time. So it technicnally works, just not during BindToClose, which triggers during an event of a global server shutdown or when a server of 1 person leaves and the game shuts down while the player is leaving.

image

Did some tweaking and got a really slow server shutdown. I guess it will do for now until I can find a better fix for it. It appears to be saving on a loop or atleast saving more than once too quickly.

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