If Player has Gamepass make GUI Button function normally, If not then prompt to buy it

Hi! I need help…

I’m trying to do a Premium Halo Gamepass for one of my games.
There will be some Free Halos on the menu and then some Paid.
I want help modifying the Equip Halo script for the Equip GUI Button on the Paid Halos.

So here is my idea:
If the Player already has the gamepass, the button will function normally:

local equipped = script.Parent.Parent.Equipped
local unequipped = script.Parent.Parent.Unequipped

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.Halo5Remotes.Equip:FireServer()
	unequipped.Visible = false
	equipped.Visible = true
end)

If player doesn’t has the gamepass, it will prompt to buy it, instead of executing the code above.

Can someone modify the code to make that happen?
Thanks! :slight_smile:

1 Like
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId,gamepassId) then

-- Code Here

else

game:GetService("MarketplaceService"):PromptGamePassPurchase(player,123456789)

end

Change 123456789 to the gamepass ID.

1 Like

Adding on what Marcus said…

local gamepassId = 000000 -- Add gamepass ID here.

if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId,gamepassId) then -- This will check if the player owns the gamepass.

-- Code Here

else

game:GetService("MarketplaceService"):PromptGamePassPurchase(player,123456789) -- This will prompt the player purchase button.

end
1 Like

Hi! Thanks for your awnsers @Mister_momp @Dev_Frags!

I tested the local script like this and it didn’t work (didn’t execute the code or didn’t prompt to buy).
Probably i’m doing something wrong.

local gamepassId = 20269713 -- Add gamepass ID here.
local equipped = script.Parent.Parent.Equipped
local unequipped = script.Parent.Parent.Unequipped

if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId,gamepassId) then -- This will check if the player owns the gamepass.

	script.Parent.MouseButton1Click:Connect(function()
		game.ReplicatedStorage.Halo5Remotes.Equip:FireServer()
		unequipped.Visible = false
		equipped.Visible = true
	end)

else

	game:GetService("MarketplaceService"):PromptGamePassPurchase(player,20269713) -- This will prompt the player purchase button.

end
1 Like

Did the output show any errors?

1 Like

Yes, it showed this:

Players.ZuperManoz.PlayerGui.HaloGui.MainFrame.AnimationsScroll.ScrollingFrame.Halo5.Equip.EquipEvent:5: attempt to index nil with ‘UserId’ - Client

1 Like
local gamepassId = 20269713 -- Add gamepass ID here.
local equipped = script.Parent.Parent.Equipped
local unequipped = script.Parent.Parent.Unequipped



script.Parent.MouseButton1Click:Connect(function()
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId,gamepassId) then -- This will check if the player owns the gamepass.
		game.ReplicatedStorage.Halo5Remotes.Equip:FireServer()
		unequipped.Visible = false
		equipped.Visible = true
	else
		game:GetService("MarketplaceService"):PromptGamePassPurchase(player,20269713) -- This will prompt the player purchase button.
	end
end)

Try this instead.

2 Likes

Nevermind my reply, you forgot to add a variable for the player, add this:

local player = game.Players.LocalPlayer

3 Likes

Thanks! Now it worked…
And in line 14 I can change the 20269713 to the gamepassId variable right?

1 Like

Change the “20269713” to gamepassId, then change the gamepassId variable to what your gamepass ID is, in case you want to easily change it later.

2 Likes

I just did that! Thanks again! :slight_smile:

2 Likes