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
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)