We use cookies on this site to enhance your user experience

HasGamePassChecker

HasGamePassChecker

Sep 14 2018, 5:03 PM PST

local HasGamepassChecker = {}

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

local MARKETPLACE_RETRIES = 5
local RETRY_DELAY = 0.5
local RETRY_FUNCTION_NAME = "GamepassRetryFunction"

-- Setup the RemoteFunction to communicate between server scripts and
-- any client it needs to send notifications to
local retryRequest
if RunService:IsServer() then
	retryRequest = ReplicatedStorage:FindFirstChild(RETRY_FUNCTION_NAME)
	if not retryRequest then
		retryRequest = Instance.new("RemoteFunction")
		retryRequest.Name = RETRY_FUNCTION_NAME
		retryRequest.Parent = ReplicatedStorage
	end
else
	retryRequest = ReplicatedStorage:WaitForChild(RETRY_FUNCTION_NAME)
end
HasGamepassChecker.retryRequest = retryRequest

local function getPlayerHasPassRetry(playerId, passId)
	local tries = 0
	local success = true
	local hasPass = nil
	repeat
		tries = tries + 1
		success = pcall(function()
			hasPass = MarketplaceService:UserOwnsGamePassAsync(playerId, passId)
		end)
	until success or tries == MARKETPLACE_RETRIES
	if not success then
		error("ERROR: Problem checking if player owns gamepass")
	end
	return hasPass
end

function HasGamepassChecker:getPlayerHasPass(playerId, passId)
	assert(RunService:IsServer())
	local success, hasPass, retry = false, false, false
	local player = Players:GetPlayerByUserId(playerId)
	repeat
		success, hasPass = pcall(getPlayerHasPassRetry, playerId, passId)
		if not success then
			retry = retryRequest:InvokeClient(player, "Can't connect to server", "Purchases may not show correctly", "Try Again")
		end
	until not retry
	
	return success and hasPass
end


return HasGamepassChecker