After receiving the prompt, I click Buy and I am able to buy the gamepass but I do not receive the reward/ability after that. I followed the wiki so I am not sure what is wrong, its not printing that I have purchased the gamepass.
Server Code:
local MarketplaceService = game:GetService("MarketplaceService")
local gamePassID = 4576507
-- Function to handle a completed prompt and purchase
local function onPromptGamePassPurchaseFinished(player, purchasedPassID, purchaseSuccess)
if purchaseSuccess == true and purchasedPassID == gamePassID then
print(player.Name .. " purchased the game pass with ID " .. gamePassID)
player.CharacterAdded:connect(function(character)
character.Humanoid.WalkSpeed = 32
end)
end
end
-- Connect 'PromptGamePassPurchaseFinished' events to the 'onPromptGamePassPurchaseFinished()' function
MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)
Client is working fine but here is the code if you want
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamePassID = 4576507
local gui = script.Parent
-- 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 == true then
print(player.Name.." already has gamepass")
else
-- Player does NOT own the game pass; prompt them to purchase
MarketplaceService:PromptGamePassPurchase(player, gamePassID)
end
end
gui.MouseEnter:connect(function()
gui.ImageColor3 = Color3.fromRGB(83, 203, 83)
end)
gui.MouseLeave:connect(function()
gui.ImageColor3 = Color3.fromRGB(102, 255, 102)
end)
gui.MouseButton1Click:connect(function()
promptPurchase()
end)
I’ve got no problems with it printing that the game pass was purchased, I’m not sure how/if that is happening. But these are 2 possible problems I’ve noticed:
When the player purchases the item, their walk speed will only change after resetting because the only place you change it is within the CharacterAdded event.
In the CharacterAdded event, you index the Humanoid immediately. I believe that it is not always accessible immediately after the character is added, use :WaitForChild() to be safe.
Addressing these issues results in this code:
local MarketplaceService = game:GetService("MarketplaceService")
local gamePassID = 4576507
-- Function to handle a completed prompt and purchase
local function onPromptGamePassPurchaseFinished(player, purchasedPassID, purchaseSuccess)
if purchaseSuccess == true and purchasedPassID == gamePassID then
print(player.Name .. " purchased the game pass with ID " .. gamePassID)
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").WalkSpeed = 32
end)
if player.Character then
player.Character:WaitForChild("Humanoid").WalkSpeed = 32
end
end
end
-- Connect 'PromptGamePassPurchaseFinished' events to the 'onPromptGamePassPurchaseFinished()' function
MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)