Grab part based off of leaderstat value

I’m trying to make it so when a player purchases a Developer Product, it highlights the button for them so they can see it.

The problem I’m having is I can’t for the life of me figure out how to make it so that the Button that gets highlighted is the one on the stage that the player is currently on.

local player = game.Players.LocalPlayer
local stageNumber = player.TeleportedStage.Value
local productId = 2659782593
local MarketPlaceService = game:GetService("MarketplaceService")

script.Parent.MouseButton1Click:Connect(function()
	MarketPlaceService:PromptProductPurchase(player, productId)
end)

MarketPlaceService.PromptProductPurchaseFinished:Connect(function(PlayerId, productId, IsPurchased)
	if IsPurchased == Enum.ProductPurchaseDecision.PurchaseGranted then
		local button = workspace.Levels:FindFirstChild()
	end
end)

My general idea is that you check if the level’s name is equal to the current stageNumber the player is on.

1 Like

Found the solution!

local player = game.Players.LocalPlayer
local productId = 2659782593
local MarketPlaceService = game:GetService("MarketplaceService")

script.Parent.MouseButton1Click:Connect(function()
	MarketPlaceService:PromptProductPurchase(player, productId)
end)

MarketPlaceService.PromptProductPurchaseFinished:Connect(function(PlayerId, productId, IsPurchased)
	if IsPurchased == true then
		local stageNumber = player.TeleportedStage.Value
		local button = workspace.Levels:FindFirstChild(stageNumber)
		print(button)
	end
end)

Actually much simpler than I thought.

Scratch that, if any product is purchased it’ll run the code.

local player = game.Players.LocalPlayer
local productId = 2659782593
local MarketPlaceService = game:GetService("MarketplaceService")

script.Parent.MouseButton1Click:Connect(function()
	MarketPlaceService:PromptProductPurchase(player, productId)
end)

MarketPlaceService.ProcessReceipt:Connect(function(PlayerId, productId, IsPurchased)
	if IsPurchased == true then
		local stageNumber = player.TeleportedStage.Value
		local button = workspace.Levels:FindFirstChild(stageNumber)
		print(button)
		local highlight = Instance.new("Highlight")
		highlight.Parent = button.Button
	end
end)

I looked up processReceipt, and I don’t know if I used it correctly or not? Like there’s no real tutorial or anything on how to use them and I’m stuck.

Process receipt must be set to a function explicitly, as shown:

game.MarketPlaceService.ProcessReceipt = function()

end)
1 Like

MarketplaceService’s ProcessReceipt isn’t an event, it’s a callback (As @Exozorcus correctly noted)

Plus, it isn’t able to be used in client-sided scripts, and the docs states that it should only be set once:

The quote was taken from here:


@qwover

This will need to be the code for the LocalScript:

-- This needs to be a LocalScript that's a direct child of the button
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local PRODUCT_ID = 2659782593

local button = script.Parent

local player = Players.LocalPlayer


button.MouseButton1Click:Connect(function()
	local success, productInfo = pcall(
		MarketplaceService.GetProductInfo, MarketplaceService,
		PRODUCT_ID, Enum.InfoType.Product)

	if success then
		if productInfo.IsForSale then
			MarketplaceService:PromptProductPurchase(player, PRODUCT_ID)
		else
			print(`Product {PRODUCT_ID} is not currently for sale`)
		end
	else
		warn(`Failed to get ProductInfo for product {PRODUCT_ID}.\nError: {productInfo}`)
	end
end)

And this will need to be the code for the server Script:

-- This needs to be a server Script in ServerScriptService
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local PRODUCT_ID = 2659782593

local levels = Workspace.Levels


MarketplaceService.ProcessReceipt = function(receiptInfo)
	local userId = receiptInfo.PlayerId
	local productId = receiptInfo.ProductId

	local player = Players:GetPlayerByUserId(userId)

	if player then
		if productId == PRODUCT_ID then
			local teleportedStage = player:FindFirstChild("TeleportedStage")

			if teleportedStage then
				local button = levels:FindFirstChild(teleportedStage.Value)

				if button then
					local highlight = Instance.new("Highlight")
					highlight.Parent = button.Button

					return Enum.ProductPurchaseDecision.PurchaseGranted
				else
					warn(`Button {teleportedStage.Value} not found inside Levels folder`)
				end
			else
				warn(`Value named TeleportedStage not found for Player {player.UserId} ({player.DisplayName})`)
			end
		end
	end

	return Enum.ProductPurchaseDecision.NotProcessedYet
end

@qwover

Alternative server Script that allows you to organize functions for multiple products:

-- This needs to be a server Script in ServerScriptService
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local levels = Workspace.Levels


local productFunctions = {
	[2659782593] = function(player, receiptInfo)
		local teleportedStage = player:FindFirstChild("TeleportedStage")

		if teleportedStage then
			local button = levels:FindFirstChild(teleportedStage.Value)

			if button then
				local highlight = Instance.new("Highlight")
				highlight.Parent = button.Button
			else
				error(`Button {teleportedStage.Value} not found inside of Levels folder`)
			end
		else
			error(`Value named TeleportedStage not found for Player {player.UserId} ({player.DisplayName})`)
		end
	end
}

MarketplaceService.ProcessReceipt = function(receiptInfo)
	local userId = receiptInfo.PlayerId
	local productId = receiptInfo.ProductId

	local player = Players:GetPlayerByUserId(userId)

	if player then
		local productFunction = productFunctions[productId]

		if productFunction then
			local success, error = pcall(productFunction, player, receiptInfo)
			if success then return Enum.ProductPurchaseDecision.PurchaseGranted end

			warn(error)
		else
			warn(`ProductFunction does not exist for product {productId}`)
		end
	end

	return Enum.ProductPurchaseDecision.NotProcessedYet
end