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

How do I make a local script in a button recognize if the player has a gamepass?

Soooo I'm kinda lost. I'm trying to have it that once it's clicked, if the player has the gamepass, it turns a button's visibility to true. I got it to prompt the gamepass but this is my first time working with gamepasses, so I'm not sure what to do.

local id = 9581363

script.Parent.MouseButton1Down:Connect(function()
	game:GetService("MarketplaceService"):PromptGamePassPurchase(game.Players.LocalPlayer,id)
end)

script.Parent.MouseButton1Click:Connect(function()
	if game:GetService("MarketplaceService"):PromptGamepassPurchaseFinishd:Connect(function(id) == true then
		script.Parent.Parent.ChangeMaterial.Visible = false
	end
end)

if anyone could help me it'd be greatly appreciated :)

1 comment

0

You misspelt "finished" in line 8.

2 answers

:UserOwnsGamepassAsync

--- LOCAL SCRIPT

local id = 9581363

	local MarketPlaceService = game:GetService("MarketplaceService")
	local Player = game.Players.LocalPlayer
			 
			
	function prompt()
		MarketPlaceService:PromptGamePassPurchase(Player, id)
	end
script.Parent.MouseButton1Click:Connect(prompt)


-------------------------------------------

--- SCRIPT, in server script service
	
	MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function(player, purchaseid, purchasesucess)
		if purchasesucess == true and purchaseid ==  id then
			player:WaitForChild("PlayerGUI"):FindFirstChild("ChangeMaterial", true).Visible = false			
			-- give the player an advantage, like 
		end
	end)
	
	if MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, id) then
		print("Player already has the gamepass")
		Player:WaitForChild("PlayerGUI"):FindFirstChild("ChangeMaterial", true).Visible = false
	end

You've got a few problems in your script.

First, don't use separate functions when you prompt the player and then deal with what happens after you prompt them.

Second, I'm confused that you set the visibility to false, even though you said that you wanted the button's visibility to be set to true.

Third, PromptGamePassPurchaseFinished is an event, not a function so you would place a period before it, not a colon. You also misspelled "Finished".

Fourth, you do not need to figure out if the player has the game pass by prompting them. I will first show you the correct way to prompt and find out whether or not the player bought the game pass; and then I will show you how to have the game check if the player already has the game pass without prompting them (which is what I think you wanted).

  1. When the player clicks on the GUI, they get prompted.
local id = 9581363

script.Parent.MouseButton1Down:Connect(function()

game:GetService("MarketplaceService"):PromptGamePassPurchase(game.Players.LocalPlayer,id) -- prompt player with game pass

-- Now this is after they have been prompted and they either hit cancel or purchased the game pass
game:GetService("MarketplaceService").PromptGamepassPurchaseFinished:Connect(function(game.Players.LocalPlayer, id, wasPurchased) -- wasPurchased is a bool value which tells you whether or not the player purchased the game pass. So if the player hit cancel, the value is false. If the player buys the game pass, then the value is true.

	if wasPurchased == true then -- if player bought the game pass
        	script.Parent.Parent.ChangeMaterial.Visible = true
	else
		return
	end
end)
end)
  1. When the player clicks on the GUI, the game will check to see if they have the game pass (I think this is what you want).
local id = 9581363
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, id) then -- UserOwnsGamePassAsync checks if the player has the game pass
	script.Parent.Parent.Visible = true -- I strongly advise you to reference this in PlayerGui instead of StarterGui because it will not become visible until the player resets
	else
		return
	end
end)

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