Help connecting button to discord webhook

Greetings, can someone help with connecting this to a discord webhook, so whenever someone purchases an item from the catalog inside my game, it will be sent to a discord channel with the following information (if possible): Player who purchased, how much the item costed, name of catalog item.

MarketService.PromptPurchaseFinished:Connect(function(player, assetId, isPurchased)
	if isPurchased == true then
		Sound2:Play()
		script.Parent.Parent.Alerts.TextBox.Text = "Successfully purchased!"
		script.Parent.Parent.Alerts.Visible = true
		script.Parent.Parent.Alerts:TweenPosition(
			UDim2.new(0.061, 0,0.738, 0),
			"In",
			"Bounce",
			.5,
			false

		)

		wait(3)
		script.Parent.Parent.Alerts:TweenPosition(
			UDim2.new(-0.237, 0,0.738, 0 ),
			"Out",
			"Bounce",
			.5,
			false

		)

		wait(3)
		script.Parent.Parent.Alerts.Visible = false

		debounce = false

Try this:

local HTTPService = game:GetService("HttpService")
local url = "Discord webhook URL here" --Look up how to create a discord webhook

MarketService.PromptPurchaseFinished:Connect(function(player, assetId, isPurchased)
	if isPurchased == true then
		Sound2:Play()
		script.Parent.Parent.Alerts.TextBox.Text = "Successfully purchased!"
		script.Parent.Parent.Alerts.Visible = true
		script.Parent.Parent.Alerts:TweenPosition(
			UDim2.new(0.061, 0,0.738, 0),
			"In",
			"Bounce",
			.5,
			false

		)

		wait(3)
		script.Parent.Parent.Alerts:TweenPosition(
			UDim2.new(-0.237, 0,0.738, 0 ),
			"Out",
			"Bounce",
			.5,
			false

		)

		wait(3)
		script.Parent.Parent.Alerts.Visible = false

		debounce = false
		-- Sending message to discord server via webhook
		local Data = {
			["content"] = player.Name.." Purchased a item!"
		}
		Data = HTTPService:JSONEncode(Data)
		HTTPService:PostAsync(url, Data)
	end
end)
3 Likes

doesn’t seem to be working unfortunately

1 Like

Here’s the full script:

local Player = game.Players.LocalPlayer
local MarketService = game:GetService("MarketplaceService")
local IDBox = script.Parent.Parent.IDInput
local ConfirmPurchase = script.Parent.Parent.confirm
local TextConfirm = script.Parent.Parent.texxt
local ID
local green = script.Parent.Parent.texxtminus
local alert = script.Parent.Parent.alert
local debounce = false
local Sound = script.Parent.Parent.Sound
local Sound2 = script.Parent.Parent.Sound2
local button = script.Parent




IDBox.FocusLost:Connect(function()
	ID = IDBox.Text
end)

ConfirmPurchase.MouseButton1Down:Connect(function()
	if not debounce then
		debounce = true
		if ID ~= nil then
			local SuccessCheck, NotValid = pcall(function()
				MarketService:PromptPurchase(Player, ID)
			end)

			if SuccessCheck then
				print(Player.Name.." has inserted a valid Catalog ID")
				debounce = false
			else
				Sound:Play()
				
				script.Parent.Parent.Alerts.TextBox.Text = "That's not a valid Catalog ID!"
				script.Parent.Parent.Alerts.Visible = true
				script.Parent.Parent.Alerts:TweenPosition(
					UDim2.new(0.061, 0,0.738, 0),
					"In",
					"Bounce",
					.5,
					false

				)

				wait(3)
				script.Parent.Parent.Alerts:TweenPosition(
					UDim2.new(-0.237, 0,0.738, 0 ),
					"Out",
					"Bounce",
					.5,
					false

				)

				wait(3)
				script.Parent.Parent.Alerts.Visible = false
				debounce = false
				
			end
		else
			--- if no value
			Sound:Play()
			alert.Visible = true
			script.Parent.Parent.Alerts.TextBox.Text = "Please enter the ID of the item in the box first."
			script.Parent.Parent.Alerts.Visible = true
			script.Parent.Parent.Alerts:TweenPosition(
				UDim2.new(0.061, 0,0.738, 0),
				"In",
				"Bounce",
				.5,
				false

			)

			wait(3)
			script.Parent.Parent.Alerts:TweenPosition(
				UDim2.new(-0.237, 0,0.738, 0 ),
				"Out",
				"Bounce",
				.5,
				false

			)
			alert.Visible = false
			wait(3)
			script.Parent.Parent.Alerts.Visible = false

			debounce = false
		end
	end

end)


local HTTPService = game:GetService("HttpService")
local url = "https://discord.com/api/webhooks/864559801950404678/d_VtddPAryzUOAGLr6lhPQYBYd6xQZoB3aMfD1ttB8eQHPaEhhOrceEuoYeWTFoyfKUX" --Look up how to create a discord webhook

MarketService.PromptPurchaseFinished:Connect(function(player, assetId, isPurchased)
	if isPurchased == true then
		Sound2:Play()
		script.Parent.Parent.Alerts.TextBox.Text = "Successfully purchased!"
		script.Parent.Parent.Alerts.Visible = true
		script.Parent.Parent.Alerts:TweenPosition(
			UDim2.new(0.061, 0,0.738, 0),
			"In",
			"Bounce",
			.5,
			false

		)

		wait(3)
		script.Parent.Parent.Alerts:TweenPosition(
			UDim2.new(-0.237, 0,0.738, 0 ),
			"Out",
			"Bounce",
			.5,
			false

		)

		wait(3)
		script.Parent.Parent.Alerts.Visible = false

		debounce = false
		-- Sending message to discord server via webhook
		local Data = {
			["content"] = player.Name.." Purchased a item!"
		}
		Data = HTTPService:JSONEncode(Data)
		HTTPService:PostAsync(url, Data)
	


	else
		Sound:Play()
		script.Parent.Parent.Alerts.TextBox.Text = "Failed to buy item!"
		script.Parent.Parent.Alerts.Visible = true
		script.Parent.Parent.Alerts:TweenPosition(
			UDim2.new(0.061, 0,0.738, 0),
			"In",
			"Bounce",
			.5,
			false

		)

		wait(3)
		script.Parent.Parent.Alerts:TweenPosition(
			UDim2.new(-0.237, 0,0.738, 0 ),
			"Out",
			"Bounce",
			.5,
			false

		)

		wait(3)
		script.Parent.Parent.Alerts.Visible = false
		debounce = false
	end
end)

May I see the output error? (If there is one)

1 Like

image

and yes, i went into a game, and purchased something but it didnt work (from the client)

1 Like

Alright, so for the discord webhook thing, I would use a remote event and a server script

  • Create a remote event in replicated storage and rename it to WebhookEvent

  • Create a server script in serverscriptservice

Put this code in the server script:

local WebhookEvent = game.ReplicatedStorage.WebhookEvent – Your webhook function

local HTTPService = game:GetService("HttpService")

local url = "Discord webhook URL here" --Look up how to create a discord webhook

WebhookEvent.OnServerEvent:Connect(function(player)
local Data = {
["content"] = player.Name.." Purchased a item!" }
Data = HTTPService:JSONEncode(Data)
HTTPService:PostAsync(url, Data)
end)

Then This in the client script:

local WebhookEvent = game.ReplicatedStorage.WebhookEvent
    MarketService.PromptPurchaseFinished:Connect(function(player, assetId, isPurchased)
    	if isPurchased == true then
    		Sound2:Play()
    		script.Parent.Parent.Alerts.TextBox.Text = "Successfully purchased!"
    		script.Parent.Parent.Alerts.Visible = true
    		script.Parent.Parent.Alerts:TweenPosition(
    			UDim2.new(0.061, 0,0.738, 0),
    			"In",
    			"Bounce",
    			.5,
    			false

    		)

    		wait(3)
    		script.Parent.Parent.Alerts:TweenPosition(
    			UDim2.new(-0.237, 0,0.738, 0 ),
    			"Out",
    			"Bounce",
    			.5,
    			false

    		)

    		wait(3)
    		script.Parent.Parent.Alerts.Visible = false

    		debounce = false
    		-- Sending message to discord server via webhook
    		WebhookEvent:FireServer()
    	end
    end)
3 Likes

Ah yes, perfect - thank you for helping me out. :slight_smile:

1 Like

youre welcome good day tophat friend!

1 Like

Haha, you too! :hugs: :tophat: