How do I save a chat tag?

Hi, I have a script that detects whenever you buy a developer product, and I want to save the players chat tag if they buy it.

Script:

local MarketplaceService =  game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local ServerScriptService = game:GetService("ServerScriptService")
local Players = game:GetService("Players")

local DataStore = DataStoreService:GetDataStore("PFStores")
local ChatService = require(ServerScriptService:WaitForChild("ChatServiceRunner").ChatService)

function giveChatTag(player)
	local tags = {
		{
			TagText = "Donator",
			TagColor = Color3.fromRGB(38, 255, 0)
			}
	}

	local speaker = nil
	while speaker == nil do
		speaker = ChatService:GetSpeaker(player.Name)
		if speaker ~= nil then break end
		task.wait(.001)
	end
	speaker:SetExtraData("Tags",tags)
	speaker:SetExtraData("ChatColor",Color3.fromRGB(226, 226, 0))
	
end

function MarketplaceService.ProcessReceipt(receiptInfo)
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)

	if player then
		giveChatTag(player)
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

Any help would be appriciated, thanks!

Why not use A Gamepass for CHAT Tag instead?
You dont need to save data at all then.

I am not using a gamepass because its for donations and I want players to repetedly purchase them.

Do you have A “Donated” Leaderstat?
image

What isn’t working with your script?

Everything works fine, I was just wondering how to use DataStores to save chat tags.

what does that have to do with this post lol. And no, I don’t

Your datastore key could be player.UserId .. "Donated" and the value should be how much they have donated, in case you want that info later.

local Players = game:GetService("Players")
local DataStore = DataStoreService:GetDataStore("PFStores")
local ChatService = require(ServerScriptService:WaitForChild("ChatServiceRunner").ChatService)

function giveChatTag(player)
	local tags = {
		{
			TagText = "Donator",
			TagColor = Color3.fromRGB(38, 255, 0)
			}
	}

	local speaker = nil
	while speaker == nil do
		speaker = ChatService:GetSpeaker(player.Name)
		if speaker ~= nil then break end
		task.wait(.001)
	end
	speaker:SetExtraData("Tags",tags)
	speaker:SetExtraData("ChatColor",Color3.fromRGB(226, 226, 0))
	
end

function MarketplaceService.ProcessReceipt(receiptInfo)
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)

	if player then
		-- stores donation amounts
		DataStore:IncrementAsync(player.UserId .. "Donations", receiptInfo.CurrencySpent)
		giveChatTag(player)
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

-- check if player donated on login
Players.PlayerAdded:Connect(function(player: Player)
	local success, value = pcall(DataStore.GetAsync, DataStore, player.UserId .. "Donated")
	while not success do
		task.wait(1)
		local success, value = pcall(DataStore.GetAsync, DataStore, player.UserId .. "Donated")
	end
	
	if value > 0 then
		giveChatTag(player)
	end
end)

Here is a comprehensive tutorial on data stores on the roblox devforums

Hi, thanks for the method, but for some reason when I donate then rejoin it doesn’t save.

might be because I messed up the key name, they need to be the same but I used

player.UserId .. "Donations" and player.UserId .. "Donated" change them both to the former.

After changing it it still doesn’t save for me