When donation dev product is purchased, it triggers all chat announcements of all the buttons instead of just one

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to achieve a system where when a user purchases a donation dev product, an announcement is made showing the server that they have donated.
  2. What is the issue? Include screenshots / videos if possible!
    When i donate, all announcements play at the same time.

    Also, im not sure if it is announcing only to the player or to the server?
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried placing the function to announce inside another function, but i am completely lost as to what to do.
    Here is my scripts:
    50 Robux donation script
    All the other robux donation scripts are the same, just with different content to announce.
local button = script.Parent
local MarketplaceService = game:GetService("MarketplaceService")
local developerProductID = 2653375420 -- Replace with your Developer Product ID
local player = game.Players.LocalPlayer
button.MouseButton1Click:Connect(function()
	
	if player then
		-- Prompt the player to purchase the developer product
		MarketplaceService:PromptProductPurchase(player, developerProductID)
		
	end
end)
MarketplaceService.PromptProductPurchaseFinished:Connect(function(UserId, ProductId, IsPurchased)
	if IsPurchased then
		local ChatNotification = require(game.ReplicatedStorage:WaitForChild("ChatNotification"))
		task.wait(3)
		ChatNotification.New("[SYSTEM]: "..player.Name.." has just donated 50 Robux!", Color3.fromRGB(255,196,60), "BuilderSans")
		game.SoundService.Money:Play()
	end
end)

Chat notification module script:

local ChatNotification = {}


local TCS = game:GetService("TextChatService")
local Channel = TCS:WaitForChild("TextChannels"):WaitForChild("RBXSystem")

local function SystemMessage(info : array)
	return '<font color="#'..info["Color"]..'"><font size="'..info["FontSize"]..'"><font face="'..info["Font"]..'">'..info["Text"]..'</font></font></font>'
end

function ChatNotification.New(text : string, color : Color3, font : Enum)
	Channel:DisplaySystemMessage(
		SystemMessage({
			Text = text, 
			Font = font,
			Color = color:ToHex(),
			FontSize = "17",
		})
	)
end
return ChatNotification

MarketplaceService.PromptProductPurchaseFinished is an objective event. You can see this through the arguments the event provides to its connected functions:

  1. The UserId of the player for whom the prompt was shown.
  2. The ID number of the developer product shown in the prompt (not to be confused with an asset ID).
  3. Indicates if the user pressed OK (true), Cancel (false), or errored (false).

If it’s not already clear, functions connected to this event will fire regardless of what developer product was prompted. For this reason, you’re provided with the ID of the developer product so you can isolate responses. To best correct your code, handle all developer product responses in one script and map the IDs of the donation developer products to their values. You’ll also need to properly derive the buying player from the given user ID.

local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage  = game:GetService("ReplicatedStorage")
local Players            = game:GetService("Players")


local MESSAGE_TEMPLATE  = "[SYSTEM]: %s has just donated %d robux!"
local MESSAGE_COLOR     = Color3.fromRGB(255,196,60)
local MESSAGE_FONT_NAME = "BuilderSans"

local PRODUCT_DONATION_MAP = {
	[2653375420]  = 50,
	[--[[Id #2]]] = --[[R$]],
	[--[[Id #3]]] = --[[R$]],
	-- And so on...
}


local ChatNotification = require(ReplicatedStorage:WaitForChild("ChatNotification"))



local function onPromptProductPurchaseFinished(userId: number, productId: number, wasPurchased: boolean)
	if not wasPurchased then
		return
	end

	local player = Players:GetPlayerByUserId(userId)
	if not player then
		return
	end

	local donation = PRODUCT_DONATION_MAP[productId]
	if not donation then
		return
	end

	local message = string.format(MESSAGE_TEMPLATE, player.Name, donation)

	-- ".new" is conventionally appropriate. You should also type "Font" as a string.
	ChatNotification.new(message, MESSAGE_COLOR, MESSAGE_FONT_NAME)
end



MarketplaceService.PromptProductPurchaseFinished:Connect(onPromptProductPurchaseFinished)

image
Ive put this script inside and modified the product IDs,
image
but now nothing comes up.

Are you experiencing any errors?

Ive checked output, it doesnt say any errors.

That’s strange. I wrote that script directly in the DevForum, so I was bound to make a mistake somewhere—I did. I have since corrected that mistake. Regardless, the mistake should’ve raised an error. Try using my edited script. If nothing happens again, we’ll need to start debugging



Found an error!
(Also, i forgot to tell you, before you changed your mistake, i changed your mistake players to game.players, but it doesnt matter now since ive replaced the script with your updated version.)

I corrected more than that. Anyways, the problem here is clear. I changed “.New” to “.new”. I elaborate on why in the comment above. I recommend you employ this change

Woohoo!! It works! Thank you :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.