How to create a button that can only be pressed by gamepass owners?

Try to make a button only accessible to a gamepass owner. This code very wrongly written, I’m aware. I have one if function embedded into another if function, which I’m pretty sure isn’t logical.

wait(0.9)

local button = script.Parent
local on = false
local musicgui = game.Players.LocalPlayer.PlayerGui.customRadio.Background

local player = game.Players.LocalPlayer

local id = 14536083

script.Parent.MouseButton1Click:connect(click)

function click()
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, id) then -- check if player has gamepass
		-- check if gui is off, if so, turn on, and vice versa
		if on == false
			on = true
			musicgui:TweenPosition(UDim2.new(0, 0, 0.218, 0),"Out","Sine",0.5) 
		else 
			on = false
			musicgui:TweenPosition(UDim2.new(-0.3, 0, 0.218, 0),"Out","Sine",0.5) 
		end	
	else
		MarketplaceService:PromptGamePassPurchase(player, ID_mod) -- if player doesn't have gamepass, prompt purchase
	end
end

Any help is greatly appreciated, just trying to learn. Thanks!

3 Likes

Im pretty bad at scripting ngl, but I’m pretty sure you could just do

if not game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, id) then return end
1 Like

If you want only to show the button for GP owners :

local button = script.Parent
local on = false
local musicgui = game.Players.LocalPlayer.PlayerGui.customRadio.Background
local MPS = game:GetService("MarketPlaceService")

local player = game.Players.LocalPlayer

local id = 14536083 
if MPS:UserOwnsGamePassAsync(player.UserId, id) then
   button.Visible = true
else
   buttton.Visible = false
end 
-- rest of script
1 Like

Yes, that was the original plan, but I didn’t want just an empty button slot on the UI. I could make it so that non-GP owners see “Purchase Custom Radio” button and GP-owners see the regular “Custom Radio” button.

local button = script.Parent
local on = false
local musicgui = game.Players.LocalPlayer.PlayerGui.customRadio.Background
local MPS = game:GetService("MarketPlaceService")

local player = game.Players.LocalPlayer

local id = 14536083 
if MPS:UserOwnsGamePassAsync(player.UserId, id) then
   button.Visible = true
   button.Text = "Custom Radio"
   -- Do whatever you want to people that have GP make (Custom the radio here)

else
   buttton.Visible = true
   button.Text = "Purchase Custom Radio"
   -- Do whatever you want to people that don't have GP make (Prompt the GP here)
end 
-- rest of script