Is there a way to detect if a player bought a gamepass?

So that I can provide the gamepass benefits real time so the player will not have to rejoin for the gamepasses to start working?

--Pseudocode
function CheckGamepasses()
    if PlayerOwnsGamePass(MyGamePass) then
        Player.ChatColor = SpecialChatColor
    end
end
CheckGamePasses()
Player.BoughtNewGamepass:Connect(CheckGamePasses)
1 Like

https://developer.roblox.com/en-us/api-reference/function/MarketplaceService/UserOwnsGamePassAsync

Try this:

local MarketplaceService = game:GetService("MarketplaceService")
local ChatServiceRunner = require(game.ServerScriptService:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
local Players = game:GetService("Players")
local GamepassID = 0
local SpecialChatColor = Color3.fromRGB(255, 233, 67)

local function gamepassPurchaseFinished(player, gamepassId, wasPurchased)
	if wasPurchased and gamepassId == GamepassID then
		local speaker = ChatServiceRunner:GetSpeaker(player.Name)
		if speaker then
			speaker:SetExtraData("ChatColor", SpecialChatColor)
			speaker:SetExtraData("NameColor", SpecialChatColor)
		end
	end
end

function CheckGamepasses(speakerName)
	local speaker = ChatServiceRunner:GetSpeaker(speakerName)
	local player = speaker:GetPlayer()
	if speaker and player then
		local success, ownsGamepass = pcall(MarketplaceService.UserOwnsGamePassAsync, MarketplaceService, player.UserId, GamepassID)
		if success and ownsGamepass then
			speaker:SetExtraData("ChatColor", SpecialChatColor)
			speaker:SetExtraData("NameColor", SpecialChatColor)
		elseif not success then
			warn("An error occurred when checking if the player owns the gamepass")
		end
	end
end

ChatServiceRunner.SpeakerAdded:Connect(CheckGamepasses)
MarketplaceService.PromptGamePassPurchaseFinished:Connect(gamepassPurchaseFinished)
1 Like

Will MarketplaceService.PromptGamePassPurchaseFinished fire even if someone buys a gamepass from the website?

I think that event only fires if it was purchased in game. but if the player bought it from the website and rejoins the experience it should work.