Changing Player Stats in a DataStore

Hello. I’m trying to figure out how to change and add to a players “stats” with in a DataBase, but I don’t seem to be making progress, probably because me as well as my code is all over the place, let me explain what I’ve done.

This is the player data being created/found:

local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("Data")
local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
local e = require(game:GetService("ServerStorage").Functions)

Players.PlayerAdded:Connect(function(player)
	local PlayerData = Instance.new("Folder", player)
	PlayerData.Name = "PlayerData"
	
	local LevelCompletions = Instance.new("Folder", PlayerData)
	LevelCompletions.Name = "LevelCompletions"
	
	local L0 = Instance.new("IntValue", LevelCompletions)
	L0.Name = "Level 0 Completions"
	local L1 = Instance.new("IntValue", LevelCompletions)
	L1.Name = "Level 1 Completions"
	
	local L0Data = nil
	local L1Data = nil
	local success, err = pcall(function()
		L0Data = Data:GetAsync(tostring(player.UserId), L0)
		L1Data = Data:GetAsync(tostring(player.UserId), L1)
	end)
	if success then
		L0.Value = L0Data
		L1.Value = L1Data
		print("Successfully Grabbed "..player.Name.."'s Data!")
	else
		warn(err)
	end
end)

This is the function that is called when the server is invoked that is responsible for adding 1 to a IntValue of theirs:

local functions = {}

function functions.UpdateData(player)
	local DataStoreService = game:GetService("DataStoreService")
	local Data = DataStoreService:GetDataStore("Data")
	
	local PlayerData = player.PlayerData
	local L0 = player.PlayerData.LevelCompletions["Level 0 Completions"].Value
	local L1 = player.PlayerData.LevelCompletions["Level 1 Completions"].Value
	local L0Data = nil
	local L1Data = nil
	
	L0Data = Data:GetAsync(tostring(player.UserId), L0)
	L1Data = Data:GetAsync(tostring(player.UserId), L1)
	
	Data:UpdateAsync(player.UserId, function(Data)
		L0Data = L0 + 1;
	end)
	print(player.Name)
end

return functions

If I need to explain or elaborate more on what I’m trying to accomplish, I will.

NOTE: Completely unfamiliar with DataStores.

If I understood you right, you’re trying to change someone’s data while he’s in game? like giving him cash every 5 minutes or something alike?

Let say they completed something, it’ll run a function to add a “Level Completion” to their stats, which yes, is fairly similar I’d say.

I’d recommend you create a folder [ for all levels in your game] and put it inside the player when he joins your game, now, you have several ways to continue from here.

One way is to check whenever the player has leveled/completed a mission, and once he’s done, fire a remote [ if its from the client] to the server, which will create a Boolean into your folder, and since he’s completed it, you’d set its value to true. [name those booleans as your mission names, it’d help later on]

Now, you can save the player’s folder and booleans and reload them with their value when the player has joined again [ and he has data],
and with that, you can check for every player if he has completed a mission, and if he did, dont let him do it again [ by checking its value on the folder].

Well, I plan on letting the player redo the level as many times as they’d like.

The other thing is, why can’t I do it the way I have done it so far? (which is having one folder, containing all IntValues for Level Completions inside of that?

You absolutely can, as long as your datestore works and saves their data.

It does, my problem right now is adding/changing them when called upon a function.

Or at least I think it does, if I’m struggling to even find/locate the stats in the datastore.

If you want them to do that mission for how many times they want, one way is to add an int value inside of each ‘Level Completed’ value, name it 'TimesCompleted, and for each time the player has completed it, just add to that +1

I’m aware, but right now I’m figuring out how to even modify one of the Map Completion values inside a DataStore for the player.

You can update their states outside of the data script,
if I understood you right, and you want to change the player’s mission value, you can do it on another script , while you access to that folder and those values accordingly.

Why? is the stats going to save when they log off? Like the function I have above that should update it but it doesn’t.

There are several articles about DataStore, if you want to understand it 100%, here are some:

I mean, I’ve looked over these briefly but I don’t think it addresses my problem directly.

Have you watched some videos about DataStore?

AlvinBlox,TheDevKing, and many others have made great tutorials on that, I am sure one of them will help you.

Best wishes!