I want to understand why UserOwnsGamePassAsync doesn’t work and returns the opposite value.
I have made test-purchase in studio and I get the item but then when I equip it, it get’s destroyed because the tool thinks I do not have the gamepass even though I do.
I have tried to search for solutions on the forum but methods didn’t fix my issue.
So this is the code for the tool that can only be used if the player has the gamepass. Any help is greatly appreciated!
local Tool = script.Parent
local Player = Tool.Parent.Parent
local Humanoid = Player.Character.Humanoid
local Pass = 123123 -- For demo purposes
local MarketplaceService = game:GetService("MarketplaceService")
Tool.Equipped:Connect(function()
if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, Pass) then
Humanoid.WalkSpeed += 14
else
Tool:Destroy()
end
end)
Tool.Unequipped:Connect(function()
Humanoid.WalkSpeed -= 14
end)
It is written right next that it’s for demonstration purposes. I have the real correct ID when I tested it. The problem is on the MarketplaceService:UserOwnsGamePassAsync.
You could set an attribute to the player when the gamepass is bought, and instead of checking with the MarketPlaceService:UserOwnsGamePassAsync() use an attribute. Here is an example that you could try using.
local Tool = script.Parent
local Player = Tool.Parent.Parent
local Humanoid = Player.Character.Humanoid
local Pass = 123123 -- For demo purposes
local MarketplaceService = game:GetService("MarketplaceService")
local GamepassAttributeName = "HasToolGamepass" --/// Change it to any name you like, should be a boolean attribute
Tool.Equipped:Connect(function()
if Player:GetAttribute(GamepassAttributeName) == true then
Humanoid.WalkSpeed += 14
else
Tool:Destroy()
end
end)
Tool.Unequipped:Connect(function()
Humanoid.WalkSpeed -= 14
end)
Now, all you have to do is add this attribute in two cases:
When the player joins and has the gamepass
When the player buys the gamepass
If you already have a script that checks when a player join, add the UserOwnsGamePassAsync check into it, and add the attribute if it returns true. It could look like this
local Pass = 123123 -- /// Change this to the actual gamepass id
local GamepassAttributeName = "HasToolGamepass" --/// Change it to any name you like, should be a boolean attribute
game.Players.PlayerAdded:Connect(function(Player: Player)
if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, Pass) == true then
Player:SetAttribute(GamepassAttributeName, true)
end
end)
When a player buys the gamepass, you should detect it by using the marketplace method " PromptGamePassPurchaseFinished"
local Pass = 123123 -- /// Change this to the actual gamepass id
local GamepassAttributeName = "HasToolGamepass" --/// Change it to any name you like, should be a boolean attribute
local function onGamepassPurchaseFinished(Player: Player, GamepassID: number, WasPurchased: boolean)
if GamepassID == Pass and WasPurchased == true then
Player:SetAttribute(GamepassAttributeName, true)
end
end
MarketplaceService.PromptGamePassPurchaseFinished:Connect(onGamepassPurchaseFinished)