Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Trigger the MarketplaceService when Part is Touched?

Asked by 4 years ago

(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)

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 pidgey 548 — 4y

1 answer

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
4 years ago
Edited 4 years ago

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.

0
Thanks! But the "window" with the Purchase appear only the first time. Why? GuerraReturns 122 — 4y
0
I thought that is what you wanted? Instead of removing the part, other players may want to use it? sleazel 1287 — 4y
0
People can open this shop always, but when only one purchase this item, the part desappear GuerraReturns 122 — 4y
Ad

Answer this question