Help With a GamePass Door

Hi, my name is marc im a begginer scripting.
I’m making a simulator and i wanted to add a vip door that opens when u have the gamepass.
if u have already have the gamepass and join the game the door will destroy correctly, I made too that if u touch the door without having the gamepass it prompts to buy it,and the problem comes now when i buy the door dont destroy (i think U need to rejoin but im trying it on studio) but anyways i dont want the player to rejoin i want that after purchasing it the door destroys correctly.
I think i made the code too long but like i said im just a begginer.
Heres the code:

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

local vipDoor = game.Workspace:FindFirstChild("VipDoor")

local function handleDoorForPlayer(player)
    local success, hasPass = pcall(function()
        return MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassID)
    end)

    if success then
        if hasPass then
            if vipDoor then
                vipDoor:Destroy()
                print("Door destroyed for player", player.Name)
            else
                print("Door not found when checking for player", player.Name)
            end
        else
            print("Player does not own the Game Pass", player.Name)
        end
    else
        warn("Error checking Game Pass ownership for player", player.Name)
    end
end

Players.PlayerAdded:Connect(function(player)
    handleDoorForPlayer(player)
end)

if vipDoor then
    vipDoor.Touched:Connect(function(hit)
        local character = hit.Parent
        local humanoid = character:FindFirstChildOfClass("Humanoid")

        if humanoid then
            local player = Players:GetPlayerFromCharacter(character)
            if player then
                MarketplaceService:PromptGamePassPurchase(player, gamepassID)
            end
        end
    end)
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, purchasedPassID, purchaseSuccess)
    print("Purchase finished for player", player.Name, "Success:", purchaseSuccess, "PurchasedPassID:", purchasedPassID)
    
    if purchaseSuccess and purchasedPassID == gamepassID then
        task.wait(2)
        handleDoorForPlayer(player)
    else
        print("Purchase failed or incorrect Game Pass ID", purchasedPassID)
    end
end)
1 Like

Hi, maybe :Destroy() the door after a successful gamepass purchase, I really dont see a point running the function

You are destroying the door on the server, not the client, so it will get destroyed for everyone, regardless of which player you pass as an argument. I don’t recommend destroying the door either as it’s just no practical. What I would recommend you to do is just set the CanCollide to false when a player with the game pass touches the door. Shift the pcall to the touched event also

if vipDoor then
    vipDoor.Touched:Connect(function(hit)
        local character = hit.Parent
        local humanoid = character:FindFirstChildOfClass("Humanoid")

        if humanoid then
            local player = Players:GetPlayerFromCharacter(character)
            if player then
                local function handleDoorForPlayer(player)
    local success, hasPass = pcall(function()
        return MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassID)
    end)
if not haspass then MarketplaceService:PromptGamePassPurchase(player, gamepassID)
            end
        end
      else
           vipDoor.CanCollide = false
           wait(.5)
           vipDoor.CanCollide = true
      end
    end)
end

Sorry that’s it’s ugly, I wrote this on my phone

1 Like

sorry for replying late,I thought no one gonna reply anyways tysm for helping

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