Checking gamepasses on the Server?

I’ve been trying to get my auto ranking centre to work but every time it checks for gamepasses it never works ;_; and shows the message I created: “You do not own any gamepasses, teleporting.”

(I’ve tried marketplace service with “data-item-id” as the id)
(I’ve tried marketplace service with normal gamepass id)
(I’ve tried gamepass service with normal gamepass id)

Gamepass: 4 | Receptionist - Roblox

local replicatedStorage = game:GetService("ReplicatedStorage")
local remote = replicatedStorage:WaitForChild("Event")

game.Players.PlayerAdded:Connect(function(player)
	local server = require(script.Parent.ModuleScript)
	local domain = 'not revealing' -- Make sure there is no http:// in here!
	local key = 'not revealing'
	local groupId = 3195440
	local userId = game:GetService("Players"):GetUserIdFromNameAsync(player)
	local api = server(domain, key, groupId)
	local message = "You do not own any gamepasses, teleporting."
	
	local kick = false
	local val = player:IsInGroup(3195440)
	if val == false then
		kick = true
		message = "Please join the group!"
		wait(1)
		remote:FireClient(player, message)
	end
	
	wait(1)
	if MarketplaceService:PlayerOwnsAsset(player, 4294617642) == true then
		print("test")
		api.setRank(player.UserId, 100)
		message = "You have been successfully ranked, teleporting."
	end
	wait(1)
	if MarketplaceService:PlayerOwnsAsset(player, 4294671778) == true then
		api.setRank(player.UserId, 101)
		message = "You have been successfully ranked, teleporting."
	end
	wait(3)
	remote:FireClient(player, message, kick)
end)
2 Likes

Use MarketplaceService:UserOwnsGamePassAsync() with the gamepass id.

3 Likes

Please be aware that this function can error. So do something like this…

local function ownsGamePass(userId, gamePassId)
    local ok, response = pcall(function() return game:GetService("MarketplaceService"):UserOwnsGamePassAsync(userId, gamePassId) end) 
    if ok then
       if type(response) == "boolean" then  
           return response;
       else 
           warn(response);
       end
    end

    return false;
end 

if ownsGamePass(1, 81239) then 
    print("User has our pass !");
end
1 Like

You can really reduce that pcall by not having to create a new function for it like so:

local mps = game:GetService("MarketplaceService")
local ok, response = pcall(mps.UserOwnsGamePassAsync, mps, userId, gamePassId) 
--[[ If it's called with : then you'll need to call it with .
Since you call it with . you'll need to include what it belongs to ]]

Creating a new function for pcall when you don’t need to is quite inefficient.

2 Likes

Re-writing this piece of code for each gamepass you will be implementing in your game, which will result in duplicate code and will be hard to maintain in the future, is inefficient. It makes sense to write a backend/framework level “Player Owns Gamepass” function that you can use every time you implement a gamepass, and this is how all well-built code bases implement this.

1 Like

Yeah, when did everyone suddenly start recommending this unreadable method pcall style??

If you’re calling a function which hits the network the cost of creating that extra lambda function is literally irrelevant in comparison to the cost of that network send / recieve.

Do you know just how many temporary Vector3 objects most math code is burning though? If you want to reduce allocations look there, not at your pcalls.

2 Likes