Med Written By howmanysmaII

UserOwnsGamePassAsync Created on: 01-01-2019

How to check if a player owns a specific gamepass.

This short tutorial will go over how to check if a user owns a gamepass using MarketplaceService's function UserOwnsGamePassAsync. It'll also cover using pcall for yielding functions such as this one.

Setup

To setup this, insert a ModuleScript into ReplicatedStorage. I'm going to call mine UserOwnsGamePassAsync, since that's what the returning function is named. Once you've done that, open the ModuleScript you created and type the following line.

local MarketplaceService = game:GetService("MarketplaceService")

What this does is gets the service named MarketplaceService for you. You can then use this variable anytime you need MarketplaceService. Next up, type out the function like so.

local MarketplaceService = game:GetService("MarketplaceService")

local function UserOwnsGamePassAsync(Player, GamePassID)
end

This is where we're going to be putting all the code for checking gamepass ownership. We're gonna type out the first line in the function now.

local MarketplaceService = game:GetService("MarketplaceService")

local function UserOwnsGamePassAsync(Player, GamePassID)
	local Success, Result = pcall(MarketplaceService.UserOwnsGamePassAsync, MarketplaceService, Player.UserId, GamePassID)
end

What this is doing is running the code, but it's running it so that if something goes wrong (like the gamepass doesn't exist, or if the player doesn't exist, or anything else), the script won't break entirely. It's kind of like a safety net for your code.

local MarketplaceService = game:GetService("MarketplaceService")

local function UserOwnsGamePassAsync(Player, GamePassID)
	local Success, Result = pcall(MarketplaceService.UserOwnsGamePassAsync, MarketplaceService, Player.UserId, GamePassID)
	return Success and Result or false
end

What this code is doing is returning the boolean of the player's ownership of the gamepass (either true or false) if the function didn't fail, but it'll return false if the program failed.

local MarketplaceService = game:GetService("MarketplaceService")

local function UserOwnsGamePassAsync(Player, GamePassID)
	local Success, Result = pcall(MarketplaceService.UserOwnsGamePassAsync, MarketplaceService, Player.UserId, GamePassID)
	return Success and Result or false
end

return UserOwnsGamePassAsync

And since we're using a ModuleScript, we have to return something, in this case, we're returning UserOwnsGamePassAsync.

Usage

To use the code, simply require the ModuleScript in ReplicatedStorage, and call it whenever you need it.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserOwnsGamePassAsync = require(ReplicatedStorage:WaitForChild("UserOwnsGamePassAsync"))

local function PlayerAdded(Player)
	local GamePassCheck = UserOwnsGamePassAsync(Player, 1234567)
	print(GamePassCheck)
end

Players.PlayerAdded:Connect(PlayerAdded)

UserOwnsGamePassAsync, with Promises!

This section is much more complicated than the previous, but it showcases lpghatguy's Promise library. It requires an external ModuleScript for this code to work however.

local MarketplaceService = game:GetService("MarketplaceService")
local Promise = require(script:WaitForChild("Promise")) -- The reason we're not requiring the module straight from the website is because
-- this code will become unusable on the client if we do. The model ID for it is 2996768439.

Now we're going to enter two functions, the regular UserOwnsGamePassAsync function from before, and a function that acts a function environment for Promises.

local MarketplaceService = game:GetService("MarketplaceService")
local Promise = require(script:WaitForChild("Promise"))

local function PromiseFenv(Player, GamePassID)
end

local function UserOwnsGamePassAsync(Player, GamePassID)
end

return UserOwnsGamePassAsync

I'm going to start off with the code in UserOwnsGamePassAsync first, since it's the shortest code and simplest to explain.

local MarketplaceService = game:GetService("MarketplaceService")
local Promise = require(script:WaitForChild("Promise"))

local function PromiseFenv(Player, GamePassID)
end

local function UserOwnsGamePassAsync(Player, GamePassID)
	return Promise.new(PromiseFenv(Player, GamePassID))
end

return UserOwnsGamePassAsync

What this is doing is returning a new Promise that calls the PromiseFenv function. Let's create the code for PromiseFenv now.

local MarketplaceService = game:GetService("MarketplaceService")
local Promise = require(script:WaitForChild("Promise"))

local function PromiseFenv(Player, GamePassID)
	return function(Resolve, Reject)
		spawn(function() -- We have to spawn in a new thread because Promise doesn't like yielding functions.
			local Success, Result = pcall(MarketplaceService.UserOwnsGamePassAsync, MarketplaceService, Player.UserId, GamePassID)
			if Success then
				Resolve(Result)
			else
				Reject(Result)
			end
		end)
	end
end

local function UserOwnsGamePassAsync(Player, GamePassID)
	return Promise.new(PromiseFenv(Player, GamePassID))
end

return UserOwnsGamePassAsync

Usage, with Promises!

Using this code is more verbose as well, but I still think it's a neat concept. The code is almost the same, it's just a little longer.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserOwnsGamePassAsync = require(ReplicatedStorage:WaitForChild("UserOwnsGamePassAsync"))

local function PlayerAdded(Player)
	UserOwnsGamePassAsync(Player, 1234567)
		:andThen(function(Ownership)
			print(Ownership)
		end)
		:catch(function(Error)
			warn("UserOwnsGamePassAsync has failed! Error:", Error)
		end)
end

Players.PlayerAdded:Connect(PlayerAdded)

Pretty simple, right?

Med Written By howmanysmaII

See howmanysmaII's profile on Roblox

Discussion