DataStore not working

It properly creates all of the Frames and Values, but it doesn’t save the data. When I change some Values, it’s at the default value again after re-joining.

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

local gameDataStore = DataStoreService:GetDataStore("ConstructionEvent") 

local function saveData(p)
	local tableToSave = {
		p.ConstructionFolder.ResourcesFolder.ResourcesX.Value;
		p.ConstructionFolder.JackhammerFolder.JackhammerX.Value;
		p.ConstructionFolder.LightsFolder.LightsX.Value;
		p.ConstructionFolder.MixersFolder.MixersX.Value;
		p.ConstructionFolder.PillarsFolder.PillarsX.Value;
		p.ConstructionFolder.HasAcceptedYet.Value;
		p.ConstructionFolder.Lock2.Value;
		p.ConstructionFolder.Lock3.Value;
		p.ConstructionFolder.Lock4.Value;
		p.ConstructionFolder.Lock5.Value;
	}
	
	local success, errorMessage = pcall(gameDataStore.SetAsync, gameDataStore, p.UserId, tableToSave)
	
	if success then
		print("CONSTRUCTION EVENT DATASTORE: Data has been saved!")
	else
		print("CONSTRUCTION EVENT DATASTORE: Data hasn't been saved!")
	end
end

Players.PlayerAdded:Connect(function(p)
	repeat wait() until p.Character
	local ConstructionFolder = Instance.new("Folder")
	ConstructionFolder.Name = "ConstructionFolder"
	ConstructionFolder.Parent = p
	
	local TypewriterDebounce = Instance.new("BoolValue")
	TypewriterDebounce.Name = "TypewriterDebounce"
	TypewriterDebounce.Parent = ConstructionFolder
	
	local HasAcceptedYet = Instance.new("BoolValue")
	HasAcceptedYet.Name = "HasAcceptedYet"
	HasAcceptedYet.Parent = ConstructionFolder
	
	local dialogueDebounce = Instance.new("BoolValue")
	dialogueDebounce.Name = "dialogueDebounce"
	dialogueDebounce.Parent = ConstructionFolder
	
	local progressDebounce = Instance.new("BoolValue")
	progressDebounce.Name = "progressDebounce"
	progressDebounce.Parent = ConstructionFolder

	local ResourcesFolder = Instance.new("Folder")
	ResourcesFolder.Name = "ResourcesFolder"
	ResourcesFolder.Parent = ConstructionFolder
	local ResourcesX = Instance.new("NumberValue")
	ResourcesX.Name = "ResourcesX"
	ResourcesX.Parent = ResourcesFolder
	local ResourcesY = Instance.new("NumberValue")
	ResourcesY.Name = "ResourcesY"
	ResourcesY.Parent = ResourcesFolder
	ResourcesY.Value = 5

	local LightsFolder = Instance.new("Folder")
	LightsFolder.Name = "LightsFolder"
	LightsFolder.Parent = ConstructionFolder
	local LightsX = Instance.new("NumberValue")
	LightsX.Name = "LightsX"
	LightsX.Parent = LightsFolder
	local LightsY = Instance.new("NumberValue")
	LightsY.Name = "LightsY"
	LightsY.Parent = LightsFolder
	LightsY.Value = 6
	
	local JackhammerFolder = Instance.new("Folder")
	JackhammerFolder.Name = "JackhammerFolder"
	JackhammerFolder.Parent = ConstructionFolder
	local JackhammerX = Instance.new("NumberValue")
	JackhammerX.Name = "JackhammerX"
	JackhammerX.Parent = JackhammerFolder
	local JackhammerY = Instance.new("NumberValue")
	JackhammerY.Name = "JackhammerY"
	JackhammerY.Parent = JackhammerFolder
	JackhammerY.Value = 3

	local MixersFolder = Instance.new("Folder")
	MixersFolder.Name = "MixersFolder"
	MixersFolder.Parent = ConstructionFolder
	local MixersX = Instance.new("NumberValue")
	MixersX.Name = "MixersX"
	MixersX.Parent = MixersFolder
	local MixersY = Instance.new("NumberValue")
	MixersY.Name = "MixersY"
	MixersY.Parent = MixersFolder
	MixersY.Value = 2

	local PillarsFolder = Instance.new("Folder")
	PillarsFolder.Name = "PillarsFolder"
	PillarsFolder.Parent = ConstructionFolder
	local PillarsX = Instance.new("NumberValue")
	PillarsX.Name = "PillarsX"
	PillarsX.Parent = PillarsFolder
	local PillarsY = Instance.new("NumberValue")
	PillarsY.Name = "PillarsY"
	PillarsY.Parent = PillarsFolder
	PillarsY.Value = 5
	
	local Lock2 = Instance.new("BoolValue")
	Lock2.Name = "Lock2"
	Lock2.Parent = ConstructionFolder
	local Lock3 = Instance.new("BoolValue")
	Lock3.Name = "Lock3"
	Lock3.Parent = ConstructionFolder
	local Lock4 = Instance.new("BoolValue")
	Lock4.Name = "Lock4"
	Lock4.Parent = ConstructionFolder
	local Lock5 = Instance.new("BoolValue")
	Lock5.Name = "Lock5"
	Lock5.Parent = ConstructionFolder
	
	local data = nil
	
	local success, errorMessage = pcall(function()
		data = gameDataStore:GetAsync(p.UserId)
	end)
	
	if success and data then
		ResourcesX.Value = data[1]
		JackhammerX.Value = data[2]
		LightsX.Value = data[3]
		MixersX.Value = data[4]
		PillarsX.Value = data[5]
		HasAcceptedYet.Value = data[6]
		Lock2.Value = data[7]
		Lock3.Value = data[8]
		Lock4.Value = data[9]
		Lock5.Value = data[10]
	else
		print("CONSTRUCTION EVENT DATASTORE: The player has no data!")
		warn(errorMessage)
	end
end)

Players.PlayerRemoving:Connect(function(p)
	saveData(p)
end)

game:BindToClose(function()
	for i, p in ipairs(game.Players:GetPlayers()) do
		task.spawn(saveData, p)
	end
end)
2 Likes

Have you tried this without being in Roblox studio? Roblox Studio doesn’t handle BindToClose very well. You can work around this if this is the case. Make sure you are in Roblox Studio and then do a time loop to save

2 Likes

I think the issue is that you’re sending the gameDataStore as an argument when setting the async. I would change the pcall to:

local success, errorMessage = pcall(function() gameDataStore:SetAsync(p.UserId, tableToSave) end)
1 Like

I advise you to use Profile Service to save data, as it is more convenient and you will only need to register 50-100~ lines of code. I advise you to watch the ProfileService guide on YouTube

ProfileService - Save your player data with ProfileService! (DataStore Module)

1 Like

Yeah, you’re right. It doesn’t work in Studio, but does work in the actual game. I also changed the pcall function by what @paetemc2 said, might have helped as well.

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