Issue with prompting a player with a game pass when touching a door

(First post very little scripting knowledge)
Question:
I want the player to be promoted with the marketplace service buy button when they come in contact with the door without owning the gamepass.

Issue:
Can’t figure out how to make the (“MarketPlaceService”):PromptProductPurchase work. It doesn’t show up when my friend who doesn’t own the gamepass runs into it.

I’ve tried looking around the forums but couldn’t find anything relating to this issue. I don’t know much about scripting so I watch a video for this script but I wanted to add the “prompt purchase” if a player doesn’t own it.

local plr = game.Players.LocalPlayer
local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassId = 9385171 

local debounce = false

script.Parent.Touched:Connect(function(hit)
 if hit.Parent:FindFirstChild("Humanoid") then
  if debounce == false then
   debounce = true
   local player = game.Players:GetPlayerFromCharacter(hit.Parent)
   if MarketPlaceService:UserOwnsGamePassAsync(player.userId, GamepassId) then
    
    print("player has the gamepass")
    script.Parent.Transparency = 1
    script.Parent.CanCollide = false
    wait(1) 
    script.Parent.Transparency = 0
    script.Parent.CanCollide = true
    wait(0.1)
    debounce = false
   else
   game.GetService("MarketPlaceService"):PromtProductPurchase(plr,GamepassId)
   print("player doesn't have gamepass")
    wait(0.5)
    debounce = false
   end
  end
 end
end)

If something is wrong with this post I’ll try my best to fix it. This is my first post.

1 Like

You put a . when you tried to get the MarketplaceService it’s a : not a . and also it’s MarketplaceService not MarketPlaceService replace it with this line of code instead and try again:

game:GetService("MarketplaceService"):PromptProductPurchase(plr,GamepassID)

Tell me if that works or if your still experiencing issues.

EDIT: This has to be a local script too not a script in order for it to work.

1 Like

Spelling error in your script!

2 Likes

Fixed. Thanks!

(30 Character limit)

2 Likes

Still seems to be not working, I transferred the code to a local script and Nothing is popping up.

1 Like

Also, going with localscript might be your best bet. Localplayer can only be accessed by localscript, hence its name.

1 Like

Game pass? Should be using PromptGamePassPurchase then, PromptProductPurchase is for developer products. PromptGamePassPurchase respects game pass ids.

Please remember to debug your code in the future first before posting threads. As you called a method with a spelling mistake, there would’ve been an error in your console telling you that you attempted to call a nil value. You can access the console via the Studio ribbon bar or F9.

2 Likes

Alright so, I have identified your problem.

What colbert said is true but he forgot about something, your trying to run a local script in Workspace and that won’t work whatsoever, so to fix this I have completely rewritten your code to be correct and removed the debounce because it’s not needed at all, so what you do is copy this script and paste it in a local script inside of StarterPlayerScripts which is located in StarterPlayer here’s the script:

local plr = game.Players.LocalPlayer
local MarketplaceService = game:GetService("MarketplaceService")
local GamepassId = 9385171 
local gamepasspart = game.Workspace.Part

gamepasspart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = plr
		local hasPass = false
		
		local success, result = pcall(function()
			hasPass = MarketplaceService:UserOwnsGamePassAsync(plr.UserId, GamepassId)
		end)
		
		if hasPass then
			print("the player has the gamepasss")
		else
			MarketplaceService:PromptGamePassPurchase(plr, GamepassId)
		end
	end
end)

IMPORTANT NOTE: Change the line

local gamepasspart = game.Workspace.Part

to be the path of where your part that you want to prompt players with this gamepass is.

And to prove that the script works here’s a GIF of me trying it out:

Don’t forget to mark my post as the solution so people can find the answer to this topic if they want to in the future. :slightly_smiling_face:

8 Likes

Changed PromptProductPurchase to PromptGamePassPurchase. Console says: [MarketplaceService:PromptGamePassPurchase() player should be of type Player, but is of type nil]

1 Like

Not necessarily that I forgot, didn’t find it important to mention. I was addressing the original implementation which kept this as a regular script. Why it was suggested this be changed to a LocalScript, I don’t know, but that’s also a workable idea provided you put the script in a container that it executes in.

@joey12489 Means your player argument isn’t defined. Prompt functions expect two arguments: a player and an asset id. If your player is nil, then you just have to make it non-nil. I don’t know what your current implementation is anymore but if it’s from a LocalScript, use game:GetService(“Players”).LocalPlayer. If it’s from a server script like your original code was, then just keep running with that and you should be fine.

1 Like

I reverted it back to a regular script. What do you mean by making the player non-nil?

1 Like

I have already told you a solution for your issue, take a look at my post above, it explains what you have to do in order for it work, I have also completely rewritten your script because it had a lot of mistakes.

2 Likes

Will the door open if the player has the VIP gamepass? I just tried it and it didn’t seem like it did. Its a vip door that needs to open


image for example of what I need. I pasted the script into a localscript and put it into StarterPlayerScript

You need to handle how the door will open on the server using a script in ServerScriptService using the PlayerAdded event using a Remote Event to send a request to make the door open for them, I made the gamepass prompt local script for you.

I solved this by doing game:GetService(“MarketplaceService”):PromptGamePassPurchase(player, GamepassId). I ultimately fixed it by replacing plr with player and using help from @colbert2677 who suggested changing PromptProductPurchase to PromptGamePassPurchase. Although @Nerkonl helped a lot and wrote a new script for me I have chose to go a different route. I’m sure I will be able to use that script in a different way, I already have an Idea so thank you Nerkonl for the script and thank you Colbert for the extra help!

1 Like