Is this a good way to check if the players owns gamepass

Include a standalone, bare-bones rbxl file with only the code you want reviewed.

  • Code Review is for reviewing specific parts of your code, and not your whole game.
  • Code Review is intended for improving already-working code. If you need help debugging your code, please use Scripting Support.

Provide an overview of:

  • What does the code do and what are you not satisfied with?
    my code is a simple gamepass check, and if the player owns the gamepass i give them the perk.
  • What potential improvements have you considered? – maybe use some other checks.
  • How (specifically) do you want to improve the code? – i want to know if this is a good way to check for gamepass, and if it’s not the best way i want to know what it is.
local MPS = game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(plr)
	--\\ Variables \\--
	
	local Char = plr.Character or plr.CharacterAdded:Wait()
	local Humanoid = Char:FindFirstChildWhichIsA("Humanoid")
	
	--\\ Gamepass Check \\--
	
	if MPS:UserOwnsGamePassAsync(plr.UserId, 8179155411) then
		wait(3)
		if not Char then
			local GamepassInfo = MPS:GetProductInfo(8179155411, Enum.InfoType.GamePass)
			game.ReplicatedStorage.CheckGamepassError:FireClient(plr, GamepassInfo.Name)
			return
		else
			if not Humanoid then
				local GamepassInfo = MPS:GetProductInfo(8179155411, Enum.InfoType.GamePass)
				game.ReplicatedStorage.CheckGamepassError:FireClient(plr, GamepassInfo.Name)
				return
			end
			Humanoid.WalkSpeed *= 2
		end
	end
	
	if MPS:UserOwnsGamePassAsync(plr.UserId, 8179153834) then
		wait(3)
		if not Char then
			local GamepassInfo = MPS:GetProductInfo(8179153834, Enum.InfoType.GamePass)
			game.ReplicatedStorage.CheckGamepassError:FireClient(plr, GamepassInfo.Name)
			return
		else
			if not Humanoid then
				local GamepassInfo = MPS:GetProductInfo(8179153834, Enum.InfoType.GamePass)
				game.ReplicatedStorage.CheckGamepassError:FireClient(plr, GamepassInfo.Name)
				return
			end
			Humanoid.JumpPower *= 2
		end
	end
	
	--\\ Charactor Died Function \\--
	
	Humanoid.Died:Connect(function()
		wait(2) -- it will wait until the player respawned (the players respawn time is 0 but i wait 2 seconds to make sure the player spawned)
		if MPS:UserOwnsGamePassAsync(plr.UserId, 8179155411) then
			wait(3)
			if not Char then
				local GamepassInfo = MPS:GetProductInfo(8179155411, Enum.InfoType.GamePass)
				game.ReplicatedStorage.CheckGamepassError:FireClient(plr, GamepassInfo.Name)
				return
			else
				if not Humanoid then
					local GamepassInfo = MPS:GetProductInfo(8179155411, Enum.InfoType.GamePass)
					game.ReplicatedStorage.CheckGamepassError:FireClient(plr, GamepassInfo.Name)
					return
				end
				Humanoid.WalkSpeed *= 2
			end
		end

		if MPS:UserOwnsGamePassAsync(plr.UserId, 8179153834) then
			wait(3)
			if not Char then
				local GamepassInfo = MPS:GetProductInfo(8179153834, Enum.InfoType.GamePass)
				game.ReplicatedStorage.CheckGamepassError:FireClient(plr, GamepassInfo.Name)
				return
			else
				if not Humanoid then
					local GamepassInfo = MPS:GetProductInfo(8179153834, Enum.InfoType.GamePass)
					game.ReplicatedStorage.CheckGamepassError:FireClient(plr, GamepassInfo.Name)
					return
				end
				Humanoid.JumpPower *= 2
			end
		end
	end)
end)

oh yea i also want to know like if the player died it also checks the gamepass to give them the speed and jump boost if its good

2 Likes

Use the CharacterAdded event, it fires every time you spawn (or respawn)

local MarketPlaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)  
		local humanoid = character:WaitForChild("Humanoid") :: Humanoid

		if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, 8179155411) then
			humanoid.WalkSpeed *= 2
		end
		if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, 8179153834) then
			humanoid.JumpPower *= 2
		end
	end)
end)

Thats how I handle that very clean works for products and gamepasses.

local market = game:GetService("MarketplaceService")
local sStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local prdFunc = {}
local passFunc = {}

prdFunc[PRODUCT ID HERE] = function(rec, plr)

	return true
end

passFunc[GAMEPASS ID HERE] = function(plr)
end

local function processReceipt(receiptInfo)
	local userId = receiptInfo.PlayerId
	local productId = receiptInfo.ProductId
	local plr = Players:GetPlayerByUserId(userId)
	if plr then
		local handler = prdFunc[productId]
		local success, result = pcall(handler, receiptInfo, plr)
		if success then
			return Enum.ProductPurchaseDecision.PurchaseGranted
		else
			--warn("Failed to process receipt:", receiptInfo, result)
		end
	end

	return Enum.ProductPurchaseDecision.NotProcessedYet
end

local function processPass(plr, passId, suc)
	if not suc then return end

	local handler = passFunc[passId]
	if handler then
		handler(plr)
	end	
end

market.ProcessReceipt = processReceipt

market.PromptGamePassPurchaseFinished:Connect(processPass)

local passes = {}
for k, v in pairs(passFunc) do
	table.insert(passes, k)
end

Players.PlayerAdded:Connect(function(plr)
	for i, passId in pairs(passes) do
		local suc, ownsPass = pcall(market.UserOwnsGamePassAsync, market, plr.UserId, passId)
		if suc and ownsPass then
			processPass(plr, passId, true)
		end
	end
end)