Skinstashery
Roblox
Home / Roblox / Studio script library / Game pass check
RB-004

Roblox game pass check script

Checks if a player owns a game pass and gives them a perk, even right after buying it.

Script type
Script
Where it goes
ServerScriptService
Category
Player data and money

For your own game in Roblox Studio. Not for use in other people's games.

The code

-- Game pass check: gives a faster walk speed to players who own the pass.

local GAME_PASS_ID = 0        -- replace with your game pass ID
local PERK_WALK_SPEED = 24    -- what owners get (the normal speed is 16)

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

local function ownsPass(player)
	if GAME_PASS_ID == 0 then
		return false
	end
	local ok, result = pcall(function()
		return MarketplaceService:UserOwnsGamePassAsync(player.UserId, GAME_PASS_ID)
	end)
	if not ok then
		warn("Could not check the game pass for " .. player.Name .. ": " .. tostring(result))
		return false
	end
	return result
end

local function applyPerk(player)
	local character = player.Character
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid.WalkSpeed = PERK_WALK_SPEED
	end
end

local function onPlayerAdded(player)
	local owns = ownsPass(player)
	player:SetAttribute("HasPass", owns)

	-- Give the perk every time the character spawns.
	player.CharacterAdded:Connect(function()
		if player:GetAttribute("HasPass") then
			applyPerk(player)
		end
	end)

	if owns then
		applyPerk(player)
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in Players:GetPlayers() do
	task.spawn(onPlayerAdded, player)
end

-- If the player buys the pass while playing, give the perk straight away.
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, passId, wasPurchased)
	if wasPurchased and passId == GAME_PASS_ID then
		player:SetAttribute("HasPass", true)
		applyPerk(player)
	end
end)

How to set it up

  1. On the Roblox Creator Dashboard, open your game, then Monetization > Passes, and create a pass. Copy its ID number.
  2. Paste the ID in place of 0 at GAME_PASS_ID.
  3. Add the Script to ServerScriptService.
  4. To let players buy it, call MarketplaceService:PromptGamePassPurchase(player, GAME_PASS_ID) from a button or ProximityPrompt script. Test in Studio: the account that made the game may need to buy it to see the perk.

Change it

More in Player data and money

All Roblox scripts

More Roblox tools