Having issues with Enum.ProductPurchaseDecision

Hey. I’m trying to make a VIP system, where the player gets prompted with a Game pass, then it checks if they bought it or not. If they bougth it a RemoteEvent should be fired, however if they didn’t buy it, another RemoteEvent should be fired. However, it seems that even when the player buys the gamepass, it fires the wrong RemoteEvent (the one which should only be fired when the player cancels the purchase)

Any help is appriciated!

local defence = game.Workspace.VipWall
local NOBUYRE = game.ReplicatedStorage.CutsceneDecline
local BUYRE = game.ReplicatedStorage.CutsceneAccept
local MarketPlace = game:GetService('MarketplaceService')
local VipOwners = {} -- who has VIP
local PASS_ID = 15256319
local HasPass = false
local cooldown = false
local Cooldown_Time = 4 -- seconds
local ProximityPrompt = defence.ProximityPrompt

local function Prompt(target)
    print("Target name:", target.Name)
        local player = target
        if cooldown == false then
            MarketPlace:PromptGamePassPurchase(player, PASS_ID)
            cooldown = true
            wait(Cooldown_Time)
            cooldown = false
        end
end

MarketPlace.PromptGamePassPurchaseFinished:Connect(function(Player, GamepassID, IsPurchased)
    print("Player:", Player)
    print("GamepassID:", GamepassID)
    print("IsPurchased:", IsPurchased)

    if GamepassID == PASS_ID then
        if IsPurchased == Enum.ProductPurchaseDecision.PurchaseGranted then
            BUYRE:FireClient(Player)
            warn('Fired PurchaseGranted RE')
        elseif IsPurchased == Enum.ProductPurchaseDecision.NoDecision then
            NOBUYRE:FireClient(Player)
            warn('Fired NoDecision RE (canceled purchase)')
        else
            print("Unexpected purchase decision:", IsPurchased)
        end
    else
        print("Unexpected GamepassID. Expected:", PASS_ID)
    end
end)




-- Triggered when the defense is touched
ProximityPrompt.Triggered:Connect(Prompt)

-- Function to update the defense state based on VIP ownership
local function UpdateDefenseState()
    local isVip = #VipOwners > 0
    -- Update the defense state based on whether there are VIP owners
    defence.CanCollide = not isVip
    if isVip then
        print("Door is locked for non-VIP players.")
    else
        print("Door is unlocked.")
    end
end

local function CheckVips(player)
    if MarketPlace:UserOwnsGamePassAsync(player.UserId, PASS_ID) then
        warn(player.Name .. ' Owns VIP!')
        if not table.find(VipOwners, player) then
            table.insert(VipOwners, player)
        
            UpdateDefenseState()
        end
    else
        warn(player.Name .. ' Doesnt own VIP!')
        -- Remove the player from VipOwners if they no longer have the game pass
        for i, owner in ipairs(VipOwners) do
            if owner == player then
                table.remove(VipOwners, i)
           
                UpdateDefenseState()
                break
            end
        end
    end
end

game.Players.PlayerAdded:Connect(CheckVips)

isPurchased is a boolean, and it is either true or false depending on if the player purchased the item. You should read the documentation before making a post next time.

Code:

local defence = game.Workspace.VipWall
local NOBUYRE = game.ReplicatedStorage.CutsceneDecline
local BUYRE = game.ReplicatedStorage.CutsceneAccept
local MarketPlace = game:GetService('MarketplaceService')
local VipOwners = {} -- who has VIP
local PASS_ID = 15256319
local HasPass = false
local cooldown = false
local Cooldown_Time = 4 -- seconds
local ProximityPrompt = defence.ProximityPrompt

local function Prompt(target)
    print("Target name:", target.Name)
        local player = target
        if cooldown == false then
            MarketPlace:PromptGamePassPurchase(player, PASS_ID)
            cooldown = true
            wait(Cooldown_Time)
            cooldown = false
        end
end

MarketPlace.PromptGamePassPurchaseFinished:Connect(function(Player, GamepassID, IsPurchased)
    print("Player:", Player)
    print("GamepassID:", GamepassID)
    print("IsPurchased:", IsPurchased)

    if GamepassID == PASS_ID then
        if IsPurchased then
            BUYRE:FireClient(Player)
            warn('Fired PurchaseGranted RE')
        else
            NOBUYRE:FireClient(Player)
            warn('Fired NoDecision RE (canceled purchase)')
        end
    else
        print("Unexpected GamepassID. Expected:", PASS_ID)
    end
end)

-- Triggered when the defense is touched
ProximityPrompt.Triggered:Connect(Prompt)

-- Function to update the defense state based on VIP ownership
local function UpdateDefenseState()
    local isVip = #VipOwners > 0
    -- Update the defense state based on whether there are VIP owners
    defence.CanCollide = not isVip
    if isVip then
        print("Door is locked for non-VIP players.")
    else
        print("Door is unlocked.")
    end
end

local function CheckVips(player)
    if MarketPlace:UserOwnsGamePassAsync(player.UserId, PASS_ID) then
        warn(player.Name .. ' Owns VIP!')
        if not table.find(VipOwners, player) then
            table.insert(VipOwners, player)
        
            UpdateDefenseState()
        end
    else
        warn(player.Name .. ' Doesnt own VIP!')
        -- Remove the player from VipOwners if they no longer have the game pass
        for i, owner in ipairs(VipOwners) do
            if owner == player then
                table.remove(VipOwners, i)
           
                UpdateDefenseState()
                break
            end
        end
    end
end

game.Players.PlayerAdded:Connect(CheckVips)

Must’ve missed it when I was looking at it on the documentation earlier. Thanks for the help!

1 Like

Yeah, now that I look at it sorry if I came off as rude. You most likely got it confused with developer products, which have the enums you used in your code.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.