How can a developer product change the parent of an object after the player has completed the purchase?

Hi, I have a problem with a script. I would like to make a product that changes the parent of an object after its purchase. This is my code:

local prompt = script.Parent
local part = script.Parent.Parent
local id = 1297086067
local service = game:GetService("MarketplaceService")

prompt.Triggered:Connect(function(plr)
	service:PromptProductPurchaseEnded(plr, id)
	game.Lighting.TeslaCoil.Parent = game.Workspace
	part:Destroy()
end)

The purchase prompt does not appear and the parent of the object does not change. I thank anyone who helps me.

I don’t quite understand what you’re trying to do here. You want a proximity prompt to show the purchase product prompt, and when it is bought, the parent of an object changes?

Yes, I don’t know how to make it.

For Dev products, you need two different scripts. The prompt script(which could be a local or server script depending on when you’re prompting) and the success script which has to be inside of ServerScriptService. Change your code to this:

local prompt = script.Parent
local part = script.Parent.Parent
local id = 1297086067
local service = game:GetService("MarketplaceService")

prompt.Triggered:Connect(function(plr)
    service:PromptProductPurchaseEnded(plr, id)
end)

And then add a new script in ServerScriptService and put this:

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

local productFunctions = {}

productFunctions[1297086067] = function(receipt, player)
	game.Ligting.TeslaCoil.Parent = game.Workspace
    part:Destroy()
		-- Indicate a successful purchase
		return true
	end
end



local function processReceipt(receiptInfo)
	local userId = receiptInfo.PlayerId
	local productId = receiptInfo.ProductId

	local player = Players:GetPlayerByUserId(userId)
	if player then
		-- Get the handler function associated with the product ID and attempt to run it
		local handler = productFunctions[productId]
		local success, result = pcall(handler, receiptInfo, player)
		if success then
			-- return PurchaseGranted to confirm the transaction.
			return Enum.ProductPurchaseDecision.PurchaseGranted
		else
			warn("Failed to process receipt:", receiptInfo, result)
		end
	end

	-- return NotProcessedYet to try again next time the player joins.
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

-- Set the callback; this can only be done once by one script on the server!
MarketplaceService.ProcessReceipt = processReceipt

Alright, let me explain what needs to be changed here. First, you will need to proximity prompt to send a ProductPurchase prompt to the player.

prompt.Triggered:Connect(function(plr)
	service:PromptProductPurchase(plr, id)
end)

Next, you need a function to detect when a player has purchased the product.

local function receipt(info)
	local plr = game.Players:GetPlayerByUserId(info.PlayerId)
	if not plr then return Enum.ProductPurchaseDecision.NotProcessedYet end
	
	if info.ProductId == id then
		game.Lighting.TeslaCoil.Parent = game.Workspace
		part:Destroy()
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

service.ProcessReceipt = receipt

Finally, add everything together.

local prompt = script.Parent
local part = script.Parent.Parent
local id = 1297086067
local service = game:GetService("MarketplaceService")

prompt.Triggered:Connect(function(plr)
	service:PromptProductPurchase(plr, id)
end)

local function receipt(info)
	local plr = game.Players:GetPlayerByUserId(info.PlayerId)
	if not plr then return Enum.ProductPurchaseDecision.NotProcessedYet end
	
	if info.ProductId == id then
		game.Lighting.TeslaCoil.Parent = game.Workspace
		part:Destroy()
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

service.ProcessReceipt = receipt

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