Using MarketplaceService to identify Gamepasses and distribute items to players


#1

I was unaware of the Gamepass overhaul that took place just this last April since none of my work had involved GP creation since then. I am now attempting to rewrite my go-to script for GP item distribution, but can’t seem to get it working. For reference, here is an original script in my game that still works thanks to Roblox keeping the old GP system intact for the time being:

Original Script

gpid = 569979204
tools = {“Coal”}

GPS = Game:GetService(“GamePassService”)
function respawned(char)
player = game.Players:FindFirstChild(char.Name)
print(“Respawned”)
if char:FindFirstChild(“Head”) ~= nil then
print(“It’s a Player!”)
if GPS:PlayerHasPass(player, gpid) then
print(“Has GPID”)
for i = 1,#tools do
game.ReplicatedStorage:FindFirstChild(tools[i]):Clone().Parent = player.Backpack
end
else
print(“No GPID”)
end
end
end
game.Workspace.ChildAdded:connect(respawned)

Here is my attempt at rewriting it, replacing the GamePassService with the MarketplaceService and running the protocol UserOwnsGamePassAsync instead of PlayerHasPass. As far as I can tell, this should be all that’s necessary; please let me know where I went wrong.

Rewritten Script

gpid = 4641115
tools = {“Bottle”}

GPS = Game:GetService(“MarketplaceService”)
function respawned(char)
player = game.Players:FindFirstChild(char.Name)
print(“Respawned”)
if char:FindFirstChild(“Head”) ~= nil then
print(“It’s a Player!”)
if GPS:UserOwnsGamePassAsync(player, gpid) then
print(“Has GPID”)
for i = 1,#tools do
game.ReplicatedStorage:FindFirstChild(tools[i]):Clone().Parent = player.Backpack
end
else
print(“No GPID”)
end
end
end
game.Workspace.ChildAdded:connect(respawned)

Thank you for your time.


#2

UserOwnsGamePassAsync takes the player’s UserId as the first argument, it doesn’t accept Player like PlayerOwnsAsset used to.


#3

So I’m supposed to find the FirstChild’s ID and not player name, then apply it to the Async protocol?


#4

There is a UserId property in the Player object. Just provide that to the function rather than the Player object. For example:

if GPS:UserOwnsGamePassAsync(player.UserId,gpid) then

#5

That did the trick, it works like a charm. Thanks for the help!