Understanding datastore & basic structure

Whats up forum, so I’m having trouble with understanding how datastores work, I read docs. etc but still don’t know where to start, so please guide me, (I also don’t wanna use third party codes so don’t suggest, wanna learn first)

local module = {}

local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("PlayerData")

local DEFAULT_TEMPLATE = {
	
	ExampleCoins = 0,
	ExampleWins = 0,
	EquippedXValue = nil,
	EquippedYValue = nil,
	OwnedXValue= {},
	OwnedXValue = {},
}

function module:SaveData(player: Player)
	local userId = player.UserId
	
--	???????
	

end


return module

Think of DataStore like Roblox saving player data in the cloud using their UserId as the key when the player joins you load their data when they leave you save their data
There are only two main things to understand GetAsync loads data and UpdateAsync saves data GetAsync reads what was saved before if nothing was saved it returns nil UpdateAsync safely updates the player data and is better than SetAsync because it reduces risk of data loss
Your DEFAULT_TEMPLATE is the default stats every new player should have when a player joins you try to load their data if it returns nil you use the template instead if it returns a table you use that table
Saving is simple you collect the player current stats into a table then call UpdateAsync using tostring player.UserId as the key and return that table so Roblox stores it
Always use pcall when calling DataStore functions because they can fail and you do not want your script to error
Basic flow is Player joins load data and give stats Player leaves save data Server shuts down save everyone again
Also you wrote OwnedXValue twice in your template so one will overwrite the other fix that first

this is an example of an average save data and load data script

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("datastore")

local function LoadData(player : Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local intvalue = Instance.new("IntValue")
	intvalue.Parent = leaderstats
	intvalue.Value = 0
	
	local success, data = pcall(function()
		return datastore:GetAsync(tostring(player.UserId))
	end)
	if success and data then
		intvalue.Value = data
		print("succesfully loaded data")
	else
		print("failed to load data")
	end
end

local function SaveData(player : Player)
	local leaderstat = player:FindFirstChild("leaderstats")
	local int : IntValue = leaderstat:FindFirstChild("Value")
	
	local success, data = pcall(function()
		datastore:SetAsync(tostring(player.UserId), int.Value)
	end)
	
	if success then
		print("succesfully saved data")
	else
		print("failed to save data")
	end
end

game.Players.PlayerAdded:Connect(LoadData)
game.Players.PlayerRemoving:Connect(SaveData)

let me break it down for you:

local function LoadData(player : Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local intvalue = Instance.new("IntValue")
	intvalue.Parent = leaderstats
	intvalue.Value = 0
	
	local success, data = pcall(function()
		return datastore:GetAsync(tostring(player.UserId))
	end)
	if success and data then
		intvalue.Value = data
		print("succesfully loaded data")
	else
		print("failed to load data")
	end
end

the function above handles and loads data safely
the code below creates a function called LoadData

local function LoadData(player : Player)

this creates the leaderboard

local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local intvalue = Instance.new("IntValue")
	intvalue.Parent = leaderstats
	intvalue.Value = 0

this is the most important part

local success, data = pcall(function()
		return datastore:GetAsync(tostring(player.UserId))
	end)

we wrap your datastore that returns the value of the key into a pcall function and return it back to the pcall function, this makes the data variable be the value of the keys

if success and data then
		intvalue.Value = data
		print("succesfully loaded data")
	else
		print("failed to load data")
	end

the if statement checks if it was a success using the first variable “success” as well as the second variable “data” and if it was a success it sets the intvalue’s value to data which is the same value as when you previously joined, the intvalue can be your coins, currency, wins or stats like the values you show in your table

lets move onto the savedata function and its similar and short to type

local function SaveData(player : Player)
	local leaderstat = player:FindFirstChild("leaderstats")
	local int : IntValue = leaderstat:FindFirstChild("Value")
	
	local success, data = pcall(function()
		datastore:SetAsync(tostring(player.UserId), int.Value)
	end)
	
	if success then
		print("succesfully saved data")
	else
		print("failed to save data")
	end
end
local leaderstat = player:FindFirstChild("leaderstats")
	local int : IntValue = leaderstat:FindFirstChild("Value")

code aboves searches for my intvalue which is like my coins

local success, data = pcall(function()
		datastore:SetAsync(tostring(player.UserId), int.Value)
	end)
	
	if success then
		print("succesfully saved data")
	else
		print("failed to save data")
	end

the datastore:SetAsync(tostring(player.UserId), int.Value) is like a method that saves data so :GetAsync() can get it

the if statement is redundant unless you want debugs

yeah so just focus on the getasync and setasync thing and we use player user id because its like your true identity

when you save data it can accept a table so saving data for each value in the table is redundant

use roblox documentation for reference not for learning

Hey so I tried to make my own but it isn’t really functional,

local module = {}

local DSS = game:GetService("DataStoreService")
local PlayerData = DSS:GetDataStore("PlayerData")

local DEFAULT_TEMPLATE = {
	
	Knuckles = 0,
	Wins = 0,
	EquippedTrail = nil,
	EquippedAura = nil,
	OwnedTrails = {},
	OwnedAuras = {},
}


function module:LoadData(player: Player)

	local Keys = {"Knuckles", "Wins"}

	local leaderboard = Instance.new("Folder")
	leaderboard.Name = "leaderstats"
	leaderboard.Parent = player

	print("LOADING DATA OF " .. player.UserId)
	local userId = player.UserId

	local success, data = pcall(function()
		return PlayerData:GetAsync(userId)
	end)

	if not success then
		
		error("SOMETHING WENT WRONG AND DATA LOAD FAILED: " .. tostring(data))
		return
	end


	if not data then
		data = {}
		for key, defaultValue in pairs(DEFAULT_TEMPLATE) do
			data[key] = defaultValue
		end

		local saveSuccess, saveErr = pcall(function()
			PlayerData:SetAsync(userId, data)
		end)

		if not saveSuccess then
			warn("Failed to initialize data for " .. userId .. ": " .. tostring(saveErr))
		end
	end


	for _, key in pairs(Keys) do
		local value = Instance.new("IntValue")
		value.Name = key
		value.Parent = leaderboard
		value.Value = data[key] or DEFAULT_TEMPLATE[key]
	end

	print("Data loaded for " .. userId, data)
end

function module:SaveData(player: Player)
	
	local userId = player.UserId
	local success, data = pcall(function()
		return PlayerData:GetAsync(userId)
	end)
	if success then
		print("Retrieved data of "..player.UserId)
		local success2, errorMsg = pcall(function()
			PlayerData:UpdateAsync(userId, data)
		end)
		if success2 then
			print("Successfully saved data of "..player.UserId)
		else
			warn("Failed to get data for player " .. player.UserId .. ": " .. errorMsg)
		end
	
	else
		warn("Failed to get data for player " .. player.UserId .. ": " .. data)
	end
	
end

return module

Can you point out my mistakes please?

1.setasync in load data (replace setasync with updateasync)
2.savedata logic is incorrect(fix savedata to use updateasync properly)
3.leaderstat only cover knuckles n wins(handle all field not js both of em)
4.error handling(use warn() instead of error() )
5.ig datastore key type : p(convert userid to string for keys)
n i got suggested improvement its up to you abt this
1.autosave loop
2.blindtoclose
3.throttling awarness

Thanks, I will surely add the features at the end they’re in my mind but for now gonna stick to the base so I learn