Script not sending webhook on purchase. Why?

I am trying to make it so that, when a product is bought, it sends a webhook with data of a textbox.

My Code:

local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local HttpService = game:GetService("HttpService")
local webhook = "https://discord.com/api/webhooks/xxxxxxxxxxxxx/xxxxxxxxxxxxxx-xxxxxxxxxx"
for i,v in pairs(Players:GetChildren()) do
	if v:IsA("Player") then
		r = v.PlayerGui.Light_Theme.bg.SponsorList.SponsorOnClick.TextBox
	end
end
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
local productFunctions = {}
productFunctions[997296821] = function(receipt, player)
   local data = {
   content = r.Text;
   username = player.Name .. " - (#" .. player.UserId .. ")";
   avatar_url = "http://www.roblox.com/Thumbs/Avatar.a..."..player.UserId
  }
  HttpService:PostAsync(webhook, HttpService:JSONEncode(data))
end
local function processReceipt(receiptInfo)
 
	local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
	local purchased = false
	local success, errorMessage = pcall(function()
		purchased = purchaseHistoryStore:GetAsync(playerProductKey)
	end)

	if success and purchased then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	elseif not success then
		error("Data store error:" .. errorMessage)
	end
 

	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then

		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	

	local handler = productFunctions[receiptInfo.ProductId]
 

	local success, result = pcall(handler, receiptInfo, player)
	if not success or not result then
		warn("Error occurred while processing a product purchase")
		print("\nProductId:", receiptInfo.ProductId)
		print("\nPlayer:", player)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	local success, errorMessage = pcall(function()
		purchaseHistoryStore:SetAsync(playerProductKey, true)
	end)
	if not success then
		error("Cannot save purchase data: " .. errorMessage)
	end
 

	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MarketplaceService.ProcessReceipt = processReceipt

The code returns:
image

Any reason why it isn’t working? I am stuck.

The error message is getting swallowed by the pcall on this line:

local success, result = pcall(handler, receiptInfo, player)

result will be an error message when success is false. Otherwise it will be whatever the function handler returns when it doesn’t error. Print result to see the error message.

image

Hi. I got this, do you understand what it means. I believe it is referring to

r = v.PlayerGui.Light_Theme.bg.SponsorList.SponsorOnClick.TextBox

I kinda jumped the gun with that one by the way. Was not sure how I would get a players local input from a server script.

Yeah so it looks like you’re trying to get the text the player writes in a textbox and send it to a webhook. The first problem is that the server can’t see what the player wrote because of FilteringEnabled. The text property won’t change to what the player wrote.

And where the error occurs:

local data = {
   content = r.Text;
   username = player.Name .. " - (#" .. player.UserId .. ")";
   avatar_url = "http://www.roblox.com/Thumbs/Avatar.a..."..player.UserId
  }

It looks like you expect ‘r’ to be the TextBox in the PlayerGui of the player who bought the product based on the code below:

for i,v in pairs(Players:GetChildren()) do
	if v:IsA("Player") then
		r = v.PlayerGui.Light_Theme.bg.SponsorList.SponsorOnClick.TextBox
	end
end

But this code which loops through all the players and sets ‘r’ to what you’re expecting doesn’t set ‘r’ for the specific player and isn’t in the same scope as the code where you index ‘r.Text’.