How can I add a chat message every time someone buys an item in my game?

I want to make it so that when someone makes a purchase in my game, a chat message is automatically sent showing the name of the user who made the purchase, the name of the purchased item, and its price.

I need help understanding how I can script this feature in my game to run effectively

I have this code but it doesnt work:

local function sendMessage(player, itemName, price)
    local message = string.format("%s ha comprado %s por %d Robux!", player.Name, itemName, price)
    game:GetService("Chat"):Chat(game.Players, message)
end

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

    if player then
        local product = game:GetService("MarketplaceService"):GetProductInfo(receiptInfo.ProductId)
        if product then
            local itemName = product.Name
            local price = receiptInfo.CurrencySpent

            sendMessage(player, itemName, price)
        end
    end
end

If someone would be so kind to help me with the script I would be very grateful!

1 Like

is this a local or server script

For a chat message, you can use StarterGui:SetCore() to make a system message locally, but you can also fire a RemoteEvent to do it on all clients.

Since I’m on vacation, it’ll take a while to type this out. Please be on standby.

1 Like
local StarterGui = game:GetService("StarterGui")

StarterGui:SetCore("ChatMakeSystemMessage", {
    Text = "Hello World!",
    Color = Color3.fromRGB(255, 0, 0),
    Font = Enum.Font.SourceSansBold,
    TextSize = 18,
})

Server:

--game.ServerScriptService.Script:

local RS = game:GetService("ReplicatedStorage")
local event = Instance.new("RemoteEvent", RS)

--Fire this remote whenever you need to.
event:FireAllClients("username","purchaseName",50)

Client:

--game.StarterGui.LocalScript:

local StarterGui = game:GetService("StarterGui")

local RS  = game:GetService("ReplicatedStorage")
local event = RS:WaitForChild("RemoteEvent")

event.OnClientEvent:Connect(function(playerName,gamepassName,gamepassPrice)
        StarterGui:SetCore("ChatMakeSystemMessage", {
            Text = playerName.." just purchased "..gamepassName.." for "..tostring(gamepassPrice).." robux!",
            Color = Color3.fromRGB(255,255,255)
            Font = Enum.Font.Arial
            FontSize = Enum.FontSize.Size24
        }
    )
end)

Hey thanks for your help, but it didn’t work for me. It seems there was a mix up. I wanted to explain that my goal was that when someone buys an accessory for their avatar in my game (not a gamepass), the message will be shown, since my game is a place full of avatars for people to get inspired or copy the avatars. thank you anyway!

You can create a remote event that fires to EVERY client. in the server script, write:

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(message)
game.TextChatService.TextChannels.RBXGeneral:DisplaySystemMessage(message)
end)