Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Trigger the MarketplaceService when Part is Touched?

(Sorry for bad English)

I have create a Part and, inside the Part, i have insert this script:

local productId = 662920300
local player = game.Players.LocalPlayer

function Shop(hit)
game:GetService("MarketplaceService"):PromptProductPurchase(player, productId)
end
script.Parent.Touched:connect(Shop)

But i have this error in the Output ->

MarketplaceService:PromptProductPurchase() player should be of type Player, but is of type nil

How i can fix this problem? (When people touch the Part, the PromptProductPurchase is triggered)

And after this... i need to remove this part after the purchase is complete from the world (or just transfer the Part in the ReplicatedStorage)

1 comment

0

Localscript's only run when they're a descendant of a Player (including their character) and in ReplicatedFirst. Not in a part in workspace

1 answer

✓ Accepted answer

Your script is a server script, and that means you can't use game.Players.LocalPlayer. That trick only works in LocalScripts. To find a player in the server script you need to use GetPlayerFromCharacter. I also made some more adjustments for You, because I am in good mood.

local MarketPlaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local promptedPlayers = {} --remember players who have been prompted
local productId = 662920300


function Shop(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then --only prompt players
		player = Players:GetPlayerFromCharacter(hit.Parent)
		if not promptedPlayers[player] then
			promptedPlayers[player] = true
			MarketplaceService:PromptProductPurchase(player, productId)
		end
	end
end
script.Parent.Touched:connect(Shop)

Have a nice scripting session.

3 comments

0

Thanks! But the "window" with the Purchase appear only the first time. Why?

0

I thought that is what you wanted? Instead of removing the part, other players may want to use it?

0

People can open this shop always, but when only one purchase this item, the part desappear

This discussion is preserved as it was. New answers, comments, and votes are closed.