Help With DataStore

I have tried EVERYTHING, EVERYTHING. And I cannot get DataStores to work completely for my game. I’ve tried numerous tutorials, nothing, AI, nothing, help from other scripters, nothing.

Here’s the script, and when I mean it doesn’t work completely I mean only “Coins” and “Gems” save, nothing else does. Exp, Level and QuestNum don’t save and I’ve looked through a ton of scripts for things that could be affecting it but nothing has worked. Please help, I’m reaching the deadline for releasing my game and I can’t without DataStore.

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

local function SaveData(Player)
	local TableToSave = {
		Player.leaderstats.Coins.Value;
		Player.leaderstats.Gems.Value;
		Player.leaderstats.Level.Value;
		Player.leaderstats.Exp.Value;
		Player.QuestNum.Value;
	}
	
	local success, errorMessage = pcall(DataStore.SetAsync, DataStore, Player.UserId, TableToSave)
	
	if success then
		print("Player data saved for "..Player.Name..".")
	else
		print("Player data not saved for "..Player.Name.."!")
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Wait()
	
	local leaderstats = Player.leaderstats
	
	local Data = nil
	
	local success, errorMessage = pcall(function()
		Data = DataStore:GetAsync(Player.UserId)
	end)
	
	if success and Data then
		leaderstats.Coins.Value = Data[1]
		leaderstats.Gems.Value = Data[2]
		leaderstats.Level.Value = Data[3]
		leaderstats.Exp.Value = Data[4]
		Player.QuestNum.Value = Data[5]
	else
		print("No player data found for "..Player.Name.."!")
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	SaveData(Player)
end)

game:BindToClose(function()
	for _, Player in ipairs(game.Players:GetPlayers()) do
		task.spawn(SaveData, Player)
	end
end)

Everything with this code seems to be perfectly fine. I’m pretty sure the problem is Roblox and its limitations with its conventional data storing service, because you’re saving a decent amount of data in your DataStore. If nothing seems to solve it, I recommend trying out DataStore2 or ProfileService, as these are the most efficient data saving modules that big Roblox studios use.

ProfileService didn’t work either.

Just adding onto this, I used to use the same datastore script as it was from a tutorial but ended up switching to ProfIleService since the datastore script was always being filled with multiple requests and never ended up saving all the needed data.

So yeah, I recommend using ProfileService.

Can we see the script you used?

For sure.

Here’s the template:

local module = {
	Data = {Coins = 0,
		Gems = 0,

		Level = 0,
		Exp = 0,

		QuestNum = 1,
		QuestInProgress = false,
		QuestStats = {
			MetPete = false,
			SticksCollected = 0,
			BoughtWire = false,
		},

		Boosts = {
			CoinsBoosts = 0,
			CoinsBoostsTime = 0,
			GemsBoosts = 0,
			GemsBoostsTime = 0,
		},

		Settings = {
			Music = true,
			SFX = true,
			World = true,
			Notifications = true,
			ShowBoosts = true,
			ShowLevel = true,
			LevelDisplay = "Round",
			ShowTime = true,
			TimeFormat = "24H",
			Weather = true,
			RealisticLighting = true,
			FancyTerrain = true,
		},

		Items = {
			FishingWire = 0,
			WateringCan = 0,
			Fertilizer = 0,
			Trails = {
				RedTrail = 0,
				OrangeTrail = 0,
				YellowTrail = 0,
				LimeTrail = 0,
				GreenTrail = 0,
				CyanTrail = 0,
				BlueTrail = 0,
				PinkTrail = 0,
				PurpleTrail = 0,
				BlueFadeTrail = 0,
			},
		},

		Updates = {
			Tutorial = true,
			BETA = false,
		},

		Codes = {
			Code1 = false,
			Code2 = false,
			Code3 = false,
		},
	}
}

return module

and what’s supposed to make it work, I changed it up a bit but still didn’t work:

-- ProfileService.lua

local module = {
	Data = {
		-- your data structure here
		Coins = 0,
		Gems = 0,

		Level = 0,
		Exp = 0,

		QuestNum = 1,
		QuestInProgress = false,
		QuestStats = {
			MetPete = false,
			SticksCollected = 0,
			BoughtWire = false,
		},

		Boosts = {
			CoinsBoosts = 0,
			CoinsBoostsTime = 0,
			GemsBoosts = 0,
			GemsBoostsTime = 0,
		},

		Settings = {
			Music = true,
			SFX = true,
			World = true,
			Notifications = true,
			ShowBoosts = true,
			ShowLevel = true,
			LevelDisplay = "Round",
			ShowTime = true,
			TimeFormat = "24H",
			Weather = true,
			RealisticLighting = true,
			FancyTerrain = true,
		},

		Items = {
			FishingWire = 0,
			WateringCan = 0,
			Fertilizer = 0,
			Trails = {
				RedTrail = 0,
				OrangeTrail = 0,
				YellowTrail = 0,
				LimeTrail = 0,
				GreenTrail = 0,
				CyanTrail = 0,
				BlueTrail = 0,
				PinkTrail = 0,
				PurpleTrail = 0,
				BlueFadeTrail = 0,
			},
		},

		Updates = {
			Tutorial = true,
			BETA = false,
		},

		Codes = {
			Code1 = false,
			Code2 = false,
			Code3 = false,
		},
	}
}

-- Load profile data from storage
local function loadProfile()
	local data = game:GetService("DataStoreService"):GetDataStore("Profile"):GetAsync("ProfileData")
	if data then
		module.Data = data
	end
end

-- Save profile data to storage
local function saveProfile()
	game:GetService("DataStoreService"):GetDataStore("Profile"):SetAsync("ProfileData", module.Data)
end

-- Update profile data
local function updateProfile(key, value)
	module.Data[key] = value
	saveProfile()
end

-- Initialize profile service
loadProfile()

-- Return the module
return {
	loadProfile = loadProfile,
	saveProfile = saveProfile,
	updateProfile = updateProfile,
	getData = function() return module.Data end,
}

I tried multiple open source version to make profileservice work including tutorials and nothing worked, this was just what I came up with (im kind of an amateur)

try using httpservice to encode and decode the data before saving and retrieving?

I have absolutely no idea how to do that :expressionless:

Is there any chance that some values, perhaps leaderstats.Level.Value, is nil? If any of the the values is nil, it will mess up your array, creating a hole in it, and unlike lua tables, json (which is used I think for saving data as well as client/server communication) follows a “strict” format of either being an array, or a dictionary.
If the table starts of as an array, it will be an array, and wont include any value that doesn’t conform to the array (string indexes, or numbers that aren’t in order, which can be caused by one of them being nil, creating a hole in the array)

Could you show us prints of the table used for SetAsync, and the table returned by GetAsync?

You could also change your table to a table that uses string indexes instead, which is more flexible, and while it takes more space in the datastore, the amount of data you are storing currently is low, and the 4mb limit is very hard to reach, you wont reach it with normal usage

I would also like to point out that your code, once working, will be vulnerable to data loss. If SaveData is called before the player’s data could load, it will overwrite the data will probably 0 (depending on what those ValueBases are set to by default). Using UpdateAsync is also recommended for ensuring two servers write one after another, and have the most recent data, which can be used to avoid data loss in other circumstances (such as a player joining another server before the previous server saved the data)

Level is set at 0, not nil. And as I said ealier Im kind of an amateur, could you do that for me?

just add print(TableToSave) before the line where SetAsync is used, and print(Data) after the line where GetAsyn() is used

If you are looking for someone to script your datastore code, you can look on the talent hub, or the #collaboration thread

I’m kinda broke but I’ll try adding the print statements

{
[1] = 5235, --what I had
[2] = 250, – what I had
[3] = 0,
[4] = 0,
[5] = 0,
}

and

TableToSave

Uh?
Did you write it as print(“TableToSave”) instead of print(TableToSave)

I can also see that here (I assume this is the table returned by GetAsync()), it actually saved Level, Exp and QuestNum, but the value is 0. Make sure the value (of the Value objects) of Level, Exp and QuestNum isn’t 0, on the server, when the saving is being done

TableToSave was from print(Data) and the table or the values was from print(TableToSave).

Would having the value of those objects set on the server when creating affect that? Although coins and gems have a set value and they save…

local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local Level = Instance.new("IntValue", Leaderstats) 
	Level.Name = "Level" 
	Level.Value = 0

	local Exp = Instance.new("IntValue", Leaderstats)
	Exp.Name = "Exp" 
	Exp.Value = 0

	local Coins = Instance.new("IntValue", Leaderstats)
	Coins.Name = "Coins" 
	Coins.Value = 0

	local Gems = Instance.new("IntValue", Leaderstats)
	Gems.Name = "Gems" 
	Gems.Value = 0

No, unless the data wasn’t loaded. When the data is loaded, it will overwrite those values

How do you update the value of the value objects? Do you do it from the client, or from the server?

It seems like the issue is from the way you update those value objects, and not from you datastore script

if success and Data then
leaderstats.Level.Value = Data[1]
leaderstats.Exp.Value = Data[2]

I mean when you level up and gain experience,

Since the value is 0, it will write 0 to the object value, which is what it is already

I have a level system in StartPlayerScripts:

game.Players.LocalPlayer:WaitForChild("leaderstats")

local Exp = game.Players.LocalPlayer.leaderstats.Exp
local Level = game.Players.LocalPlayer.leaderstats.Level

Exp:GetPropertyChangedSignal("Value"):Connect(function()
	local RequiredExp = Level.Value * 100 + 100
	
	if Exp.Value >= RequiredExp then
		Exp.Value -= RequiredExp
		Level.Value += 1
		game.ReplicatedStorage.LeaderstatsEvents.LevelUp:FireServer()
	end
end)