遊戲通行證

用戶可以購買遊戲通行證,以永久解鎖獨家內容和功能,終身有效。

創建遊戲通行證

https://create.roblox.com/
>
Dashboard
>
Creations
>
My Experiences
>
[Your Game]
Image 1
Image 2
Roblox Studio
Passes
>
Create A Pass
>
[Enter Details]
>
Create Pass
Image 1
Image 2
Roblox Studio
Back to Game Pass Page
>
Click the Created Pass
>
Sales
>
[Enter Details]
>
Save Changes
Image 1
Roblox Studio
Back to Game Pass Page
>
Copy Asset ID
Image 1
Roblox Studio

發起購買

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

-- Next Slide: checkAndPromptPurchase(player)

game.Workspace:WaitForChild("PassCheck"):WaitForChild("SurfaceGui"):WaitForChild("Purchase").Activated:Connect(
    function()
        local player = Players.LocalPlayer
        checkAndPromptPurchase(player)
    end
)
	
local function checkAndPromptPurchase(player)
    local hasPass = false
    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, passID)
    end)
    if not success then
        warn("Error while checking if player has pass: " .. tostring(message))
        return
    end
    if hasPass then
        -- Player already owns the Pass; you can display a message or take another action
        print("You already own the Game Pass!")
    else
        -- Player does NOT own the Pass; prompt them to purchase
        MarketplaceService:PromptGamePassPurchase(player, passID)
    end
end

授予遊戲通行證購買權限

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

local passID = YOUR_GAME_PASS_ID  

local function onPromptPurchaseFinished(player, purchasedPassID, purchaseSuccess)
	if purchaseSuccess and purchasedPassID == passID then
		-- logic for Game Pass
	end
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(userId, purchasedPassID, purchaseSuccess)
	local player = Players:GetPlayerByUserId(userId)
	if player then
		onPromptPurchaseFinished(player, purchasedPassID, purchaseSuccess)
	end
end)

在玩家加入時授予權限

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

local function onPlayerAdded(player)
	local hasPass = false
	-- Check if the player owns the Game Pass
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, passID)
	end)
	if not success then
		warn("Error checking pass ownership: " .. tostring(message))
		return
	end
	if hasPass then
		-- logic for Game Pass
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)