Ad Service
Summary
Methods
CreateAdRewardFromDevProductId(devProductId: number):AdReward |
GetAdAvailabilityNowAsync(adFormat: Enum.AdFormat):GetAdAvailabilityNowResult |
GetCampaignEligibilityAsync(campaignId: string,player: Player?):GetCampaignEligibilityResult |
RegisterAdOpportunityAsync(instance: Instance,placementId: number?):() |
RegisterDisclosureButton(disclosureButton: GuiButton,adIntegrationPlacementId: string):() |
RegisterImpressionSource(instance: Instance,adIntegrationPlacementId: string):() |
ShowRewardedVideoAdAsync(player: Player,reward: AdReward,placementId: number?):Enum.ShowAdResult |
ShowVideoAd():() |
UnregisterAdOpportunity(instance: Instance):() |
Events
VideoAdClosed(adShown: boolean):RBXScriptSignal |
API Reference
Methods
Create Ad Reward From Dev Product Id
Get Ad Availability Now Async
Parameters
Returns
GetAdAvailabilityNowResult
Get Campaign Eligibility Async
Returns
GetCampaignEligibilityResult
Code Samples
Example Ad Integration
local AdService = game:GetService("AdService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local YOUR_CAMPAIGN_ID = "123" -- Enter your ad integration campaign ID from ads manager here.
local AD_INTEGRATION_IDS = {
-- Enter your ad integration placement ID's for each asset you registered as part of your
-- ad integration campaign.
BrandedBuilding = "abc-123",
BrandedScreenGui = "xyz-789",
}
-- Verify that the LocalPlayer is eligible to see your campaign
local success, result = pcall(function()
return AdService:GetCampaignEligibilityAsync(YOUR_CAMPAIGN_ID)
end)
if success and result.IsEligible then
-- All ad integration assets should be hidden by default. Once you know that the
-- player is eligible, then it's safe to start showing the ad integration assets.
local brandedBuildingModel = ReplicatedStorage.adIntegration.building
local brandedBuildingModelDisclosureBillboardGui = brandedBuildingModel.disclosureBillboardGui
-- Don't forget to register your ad disclosure buttons so players know who is paying for the ad!
AdService:RegisterDisclosureButton(
brandedBuildingModelDisclosureBillboardGui.adDisclosureButton,
AD_INTEGRATION_IDS.BrandedBuilding
)
brandedBuildingModelDisclosureBillboardGui.Parent = Players.LocalPlayer.PlayerGui
-- Register a block-shaped Part that covers the visible ad content as an impression source so
-- that qualified visits can be measured for your campaign. The part can be transparent.
AdService:RegisterImpressionSource(brandedBuildingModel.impressionSourcePart, AD_INTEGRATION_IDS.BrandedBuilding)
brandedBuildingModel.Parent = game.Workspace
-- UI ad placements still need a disclosure button, but they are not currently supported as
-- impression sources.
local brandedScreenGui = ReplicatedStorage.adIntegration.screenGui
AdService:RegisterDisclosureButton(brandedScreenGui.adDisclosureButton, AD_INTEGRATION_IDS.BrandedScreenGui)
brandedScreenGui.Parent = Players.LocalPlayer.PlayerGui
endRegister Ad Opportunity Async
Returns
()
Code Samples
Track ad opportunities
local AdService = game:GetService("AdService")
local playerGui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
-- Create the ScreenGui and the Button
local screenGui = Instance.new("ScreenGui", playerGui)
local adButton = Instance.new("TextButton", screenGui)
-- Register the button with AdService
local success, err = pcall(function()
-- The second parameter is an optional Placement ID (Number) parameter generated in the Creator Hub.
-- If you choose not to provide a Placement ID, the ad opportunity is tracked under the default placement.
AdService:RegisterAdOpportunityAsync(adButton, 1234567891234567)
end)
if not success then
warn("Failed to register ad opportunity:", err)
endRegister Disclosure Button
Register Impression Source
Show Rewarded Video Ad Async
Ad Service: Show Rewarded Video Ad Async(
Returns
Code Samples
Show Rewarded Video Ads (Client)
-- In a LocalScript, in the location you want it to run:
local AdService = game:GetService("AdService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local rewardedVideoButton: GuiButton =
Players.LocalPlayer.PlayerGui:WaitForChild("myAdGui"):WaitForChild("watchRewardedVideoButton")
local watchRewardedVideoAdRemoteEvent: RemoteEvent = ReplicatedStorage:WaitForChild("watchRewardedVideoAd")
-- The client doesn't have an ad ready yet; hide the button
rewardedVideoButton.Visible = false
local success, result = pcall(function()
return AdService:GetAdAvailabilityNowAsync(Enum.AdFormat.RewardedVideo)
end)
if success and result.AdAvailabilityResult == Enum.AdAvailabilityResult.IsAvailable then
-- Show the button if an ad is avilable
rewardedVideoButton.Visible = true
end
rewardedVideoButton.Activated:Connect(function()
watchRewardedVideoAdRemoteEvent:FireServer()
-- The IsAvailable result is only good for a single ad play. To play another ad, you would have to call
-- GetAdAvailabilityNowAsync and check availability again.
rewardedVideoButton.Visible = false
end)Show Rewarded Video Ads (Server)
-- In a Script, in the location you want it to run:
local AdService = game:GetService("AdService")
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local watchRewardedVideoAdRemoteEvent: RemoteEvent = ReplicatedStorage:WaitForChild("watchRewardedVideoAd")
local REWARDED_VIDEO_PRODUCT_ID = 123 -- Enter your dev product ID here
local function grantReward(player: Player)
print(`giving a reward to {player.Name}!`)
end
MarketplaceService.ProcessReceipt = function(receiptInfo)
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
if receiptInfo.ProductId == REWARDED_VIDEO_PRODUCT_ID then
if receiptInfo.ProductPurchaseChannel == Enum.ProductPurchaseChannel.AdReward then
print(`player {receiptInfo.PlayerId} earned my rewarded video reward by watching an ad`)
else
print(`player {receiptInfo.PlayerId} purchased my rewarded video product with Robux`)
end
-- Give the player their reward
grantReward(player)
else
-- Handle other products
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
watchRewardedVideoAdRemoteEvent.OnServerEvent:Connect(function(player)
local reward = AdService:CreateAdRewardFromDevProductId(REWARDED_VIDEO_PRODUCT_ID)
AdService:ShowRewardedVideoAdAsync(player, reward)
end)Show Video Ad
Events
Video Ad Closed