Hello, Everybody
I am making a script where and if the player clicks on a gamepass inside the shop. The player is prompted with the option to purchase. That is not working tho for some weird reason. I tried running print statements on the activated event and it printed it. The gamepass ID is correct too
LOCAL SCRIPT
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamePassID = 63974938
local purchase = script.Parent
purchase.Activated:Connect(function()
-- Function to prompt purchase of the game pass
local function promptPurchase()
local player = Players.LocalPlayer
local hasPass = false
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
end)
if not success then
warn("Error while checking if player has pass: " .. tostring(message))
return
end
if hasPass then
-- Player already owns the game pass; tell them somehow
else
-- Player does NOT own the game pass; prompt them to purchase
MarketplaceService:PromptGamePassPurchase(player, gamePassID)
end
end
end)
local Players = game:GetService("Players")
local gamePassID = 63974938
local purchase = script.Parent
purchase.Activated:Connect(function()
-- Function to prompt purchase of the game pass
local function promptPurchase()
local player = Players.LocalPlayer
local hasPass = false
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
end)
if not success then
warn("Error while checking if player has pass: " .. tostring(message))
return
end
if hasPass then
print("Gamepass is owned")
-- Player already owns the game pass; tell them somehow
else
print("Gamepass not owned")
-- Player does NOT own the game pass; prompt them to purchase
MarketplaceService:PromptGamePassPurchase(player, gamePassID)
end
end
end)
I checked your code and the reason it’s not working is because you created a function that’s not fired in a function. So place that function outside .Activated and rewrite it like this:
local Players = game:GetService("Players")
local gamePassID = 63974938
local purchase = script.Parent
local MarketplaceService = game:GetService("MarketplaceService")
local function promptPurchase()
local player = Players.LocalPlayer
local hasPass = false
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
end)
if not success then
warn("Error while checking if player has pass: " .. tostring(message))
return
end
if hasPass then
print("Gamepass is owned")
-- Player already owns the game pass; tell them somehow
else
print("Gamepass not owned")
-- Player does NOT own the game pass; prompt them to purchase
MarketplaceService:PromptGamePassPurchase(player, gamePassID)
end
end
purchase.Activated:Connect(promptPurchase)
NOTE: The code will only work if the tool has a handle.