Shop script that doesn't work

Hello, I am currently struggling with a script I made.
The script is supposed to open a GUI which displays a text button and when the text button is clicked the player can purchase a gamepass.
The GUI opens without any problem but the text button does not allow the player to purchase the gamepass.
I don’t know what to do, please help me !

here the script :

local MarketplaceService = game:GetService("MarketplaceService")

local MainFrame = script.Parent.MainGui
local GravityCoilGamepass = MainFrame.MainGui.GravityCoilFrame.Price
local ShopPart = workspace.ShopOpen

local player = game.Players.LocalPlayer

local GamepassID = 10567235


GravityCoilGamepass.MouseButton1Down:Connect(function()
local success, message = pcall(function()
	HasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserID, GamepassID)
end)

if HasPass then
	print("Player have the pass")
else
	MarketplaceService:PromptGamePassPurchase(player, GamepassID)

end


ShopPart.ClickDetector.MouseClick:Connect(function()
	if not script.Parent.Frame.Visible then
		script.Parent.Frame.Visible = true
	end
end)
1 Like

I’ve forgot to say that this script is in the starter GUI and I have an other script placed in ServerScriptServive.

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local GamepassID = 10567235

game.Players.PlayerAdded:Connect(function(player)
	local success, message = pcall(function()
	HasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserID, GamepassID)
end)

if HasPass then
	print("Player have own the pass")
	
	local Gravity = game.ReplicatedStorage.GravityCoil:Clone()
	Gravity.Parent = player.Backpack

end)

local function onPromptGamePassPurchaseFinished(player, purchasePassID, purchaseSuccess)
	
	if purchaseSuccess == true and purchasePassID == GamepassID then
		print(player.Name .. "Have purchase the pass")
		
		local Gravity = game.ReplicatedStorage.GravityCoil:Clone()
	Gravity.Parent = player.Backpack
	end
	
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)
1 Like

You need to debug your code and check your console before posting threads to this category. You do not close the MouseButton1Down connection for the gravity coil game pass button. Adding another end as well as a closing parenthesis is required.

It’s also a good idea to manage your tabbing properly so you can actually catch these issues. Studio’s tabbing can be broken sometimes so if you’re working with a different text editor then they catch these issues for you.

GravityCoilGamepass.MouseButton1Down:Connect(function()
local success, message = pcall(function()
	HasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserID, GamepassID)
end)

if HasPass then
	print("Player have the pass")
else
	MarketplaceService:PromptGamePassPurchase(player, GamepassID)

end

end) -- < This needs to be here.

Also, for the server script, you have the exact same problem present, except this time you don’t close the if statement. Again, tabbing properly would help you here. Even I didn’t catch this until I read your code a second time over.

game.Players.PlayerAdded:Connect(function(player)
	local success, message = pcall(function()
	    return MarketplaceService:UserOwnsGamePassAsync(player.UserID, GamepassID)
    end)

    if success and message then
        local Gravity = game.ReplicatedStorage.GravityCoil:Clone()
	    Gravity.Parent = player.Backpack
    end
end)
1 Like
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local GamepassID = 10567235
local HasPass = false
game.Players.PlayerAdded:Connect(function(player)
	local success, message = pcall(function()
	HasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserID, GamepassID)
end)

if HasPass then
	print("Player have own the pass")
	
	local Gravity = game.ReplicatedStorage.GravityCoil:Clone()
	Gravity.Parent = player.Backpack

end

local function onPromptGamePassPurchaseFinished(player, purchasePassID, purchaseSuccess)
	
	if purchaseSuccess == true and purchasePassID == GamepassID then
		print(player.Name .. "Have purchase the pass")
		
		local Gravity = game.ReplicatedStorage.GravityCoil:Clone()
	Gravity.Parent = player.Backpack
	end
	
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)

You forgot to have an end at the if HasPass and you need a HasPass varable.

I tried to solve the script but after having gathered your solutions the “Haspass” it is found underlined with an error message


this error had already happened to me but to solve it I had to remove the “end)” at the end and which made that the script did not work.
but even after putting it back, the script still doesn’t work. Is there an error that I haven’t fixed yet ?

You need to make a variable for HasPass, under the GamepassID variable add:

local HasPass

The “HasPass” isn’t underlined anymore but the script still doesn’t work…

What is happening? Does the gamepass message get printed?

No, the message does not get printed.

Are you sure the gamepass id is correct? Also test if the mouse event gets fired by adding a print line to the beginning of the connection.

The gamepass ID is good.
then I’ve added this under the mouse click :

GravityCoilGamepass.MouseButton1Down:Connect(function()
	print("Click received")
local success, message = pcall(function()

but the message “click received” does not get printed.
(maybe because I shouldn’t have done it like that)

i would stick to something more simple like this:

GravityCoilGamepass.MouseButton1Down:Connect(function()
if MarketplaceService:UserOwnsGamePassAsync(player.UserID, GamepassID) then
print(“Player have the pass”)
else
MarketplaceService:PromptGamePassPurchase(player, GamepassID)
end
end)

I’ve put your script in the script located in the StarterGUI but it still not work and the

print("Player have the pass")

didn’t show up when I click on the text button.
I’m really confused now…

I tried to change some part of the script but it still don’t work.
I’m pretty sure that something in this script is wrong but I don’t know what…
This is the script from the StarterGui.

local MarketplaceService = game:GetService("MarketplaceService")

local MainGui = script.Parent.MainGui
local GravityCoilGamepass = MainGui.GravityCoilFrame.Price
local ShopPart = workspace.Jeff

local player = game.Players.LocalPlayer

local GamepassID = 10567235
local HasPass = false

GravityCoilGamepass.MouseButton1Down:Connect(function()
if MarketplaceService:UserOwnsGamePassAsync(player.UserID, GamepassID) then
		print("Player have the pass")
		HasPass = true
else
MarketplaceService:PromptGamePassPurchase(player, GamepassID)
end
end)

ShopPart.ClickDetector.MouseClick:Connect(function()
	if not script.Parent.MainGui.Visible then
		script.Parent.MainGui.Visible = true
	end
end)