Prompting marketplace purchase

hi, i’m looking for help with scripting a situation where upon completion of a task a user is then presented with a prompt to purchase a marketplace item.

currently i have the script awarding the player a badge upon completion. how can i then also trigger a purchase prompt? will i need to create a localscript in addition to the server script?

i’ve been browsing the forum for hours but i’m new to scripting and feel like there’s some search term i’m missing! any help would be much appreciated, please let me know if any more details are needed or if there’s already a solution for this :slight_smile:

Hii, to show a purchase prompt after awarding a badge, you’ll need two scripts: one for the server and one for the client.

In the server script, when the task is done and the badge is given, trigger an event.

-- Trigger an event after awarding the badge
game:GetService("BadgeService"):AwardBadge(player.UserId, badgeId)

-- Trigger the event for prompting purchase
game.ReplicatedStorage.PromptPurchaseEvent:Fire(player)

In the client script, listen for this event. When it’s triggered, show the purchase prompt.

-- Listen for the event to prompt purchase
game.ReplicatedStorage.PromptPurchaseEvent.OnClientEvent:Connect(function()
    -- Show the purchase prompt here
    game:GetService("MarketplaceService"):PromptPurchase(assetId)
end)

This way, the server handles the game logic, and the client manages the user interface.

1 Like

thank you for responding! i think i’m doing something wrong, because i’ve pasted your first piece about triggering the events into where i had just the badge awarded, and the client script into a new LocalScript. it brings up this in the output window:

PromptPurchaseEvent is not a valid member of ReplicatedStorage “ReplicatedStorage”

should i be naming the LocalScript? or placing it somewhere specific? or am i misunderstanding something?

thank you for your patience! i’m very inexperienced with these kinds of scripts.

It just means you need to create a new RemoteEvent in ReplicatedStorage and name it PromptPurchaseEvent

1 Like

thank you for responding! i’ve just done that, but now i get

Fire is not a valid member of RemoteEvent “ReplicatedStorage.PromptPurchaseEvent”

my apologies if this is really simple and i’m just not getting it!
ps - we have very similar display/usernames lol

Yeah that’s a mistake in the code from the previous poster, it should be :FireClient(player)

1 Like

ohhh okay, thank you! i changed that and the error stopped coming up. when testing just in studio i don’t get the prompt come up but maybe i need to test it in a game?
one last question - where does the localscript need to go? and does that need to be named as well?

It should work in Studio too.
LocalScript should be in StarterPlayerScripts

1 Like

i’ve figured it out, thank you both so much! i just had to add player here:

game:GetService(“MarketplaceService”):PromptPurchase(player,assetId)

and then the prompt came up for me in studio.
adding this here in case anyone has the same question in the future :slight_smile:

2 Likes

ahah yes sorry i was a bit tired when i wrote it but glad it helped anyway

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