Setting the value of Datastore when player joins the game

Hello!

I am wanting to script an easy datastore that I can remember by adding a table and when the player leaves it inserts their leaderstat(Coins). When the player rejoins I want it to get that value from the table and set it to the value of their leaderstats.

local Coins = {
	
	
	
}

game.Players.PlayerRemoving:Connect(function(plr)
	local coins = plr.leaderstats.Coins
	coins.Value += 20
	table.insert(Coins, coins.Value)
	print(Coins)
	
	
end)

game.Players.PlayerAdded:Connect(function(plr)
	local coins = plr:WaitForChild("leaderstats").Coins
	coins.Value = tostring(tonumber(Coins.Value))
end)

You can use the basic datastore script listed below or add it to your inventory here. Use this to build on your current script.

local DATA_STORE_NAME = "" -- Datastore name (if this is changed all data is erased as it switches to new datastore)
local DataStoreService = game:GetService("DataStoreService")

-- Function to load data from datastore
local function loadData(player, DATA_NAME)
	local DataStore = DataStoreService:GetDataStore(DATA_STORE_NAME)
	local success, result = pcall(function()
		return DataStore:GetAsync(tostring(player.UserId) .. "_" .. DATA_NAME)
	end)

	if success and result then

		return result
	else
		return "" -- Default value if no data found
	end
end

-- Function to save data to the DataStore
local function saveData(player, data, DATA_NAME)
	local DataStore = DataStoreService:GetDataStore(DATA_STORE_NAME)
	local success, result = pcall(function()
		DataStore:SetAsync(tostring(player.UserId) .. "_" .. DATA_NAME, data)
	end)

	if not success then
		warn("Failed to save data for " .. player.Name)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	-- Use this to load data when the player joins
end)