How to convert this to a server script?

I’m trying to get my local script to work on a server script.
Here’s the script:

local ID = 1

local Player = game:GetService('Players').LocalPlayer
local MarketplaceService = game:GetService('MarketplaceService')

script.Parent.MouseButton1Click:Connect(function()
	MarketplaceService:PromptProductPurchase(Player,ID)
	print("Purchased Nuke.")
end)

image
The script gives me this error when i put it on a server script:
MarketplaceService:PromptProductPurchase() player should be of type Player

Thanks :smiley:

Try to make the local script fire an event once the purchase is complete and check if the player actually bought the pass. Then activate the nuke from the server script. Hope this helps :grinning:

Make a Server Script in ServerScriptService:

local Handlers = {};

Handlers[12345] = function(Receipt, Player) -- 12345 = Product ID
	-- Nuke script
	
end;

function ProcessReceipt(p1)
	local p2 = false
	local p3, p4 = pcall(function()
	end)
	if p3 and p4 then
		return Enum.ProductPurchaseDecision.PurchaseGranted;
	else
	end;
	local p5 = game.Players:GetPlayerByUserId(p1.PlayerId);
	if not p5 then
		return Enum.ProductPurchaseDecision.NotProcessedYet;
	end;
	local handler = Handlers[p1.ProductId];
	local p6, p7 = pcall(handler, p1,p5)
	if not p6 or not p7 then
		return Enum.ProductPurchaseDecision.NotProcessedYet;
	end;
	local p3, p4 = pcall(function()
	end)
	if not p3 then
	end;
	return Enum.ProductPurchaseDecision.PurchaseGranted;
end;

game:GetService("MarketplaceService").ProcessReceipt = ProcessReceipt;
1 Like

That’s because “game.Players.LocalPlayer” isn’t defined in server scripts and will just return “nil”, meaning that you need to get the player object in some other way, you can do that with the following code:

local Players = game:GetService("Players")
local MarketplaceService = game:GetService('MarketplaceService')
local ID = 0 --change to productid

Players.PlayerAdded:Connect(function(Player)
	script.Parent.MouseButton1Click:Connect(function()
		MarketplaceService:PromptProductPurchase(Player, ID)
		print("Purchased Nuke.")
	end)
end)