Help with my gamepass script!

Hello developers, I am trying to make a VIP door, but it doesn’t work, and there aren’t any errors in script analysis or output. I also what the door to disapear for the person who bought the gamepass.

-- server script child of the VIP door
local MPS = game:GetService("MarketplaceService")
local BindableEvent = game.ReplicatedStorage.Event
BindableEvent.Event:Connect(function(ID)
    if ID == 10169496 then
script.Parent.Touched:Connect(function(hit)
	print(hit.Parent.Name)
	if hit.Parent:FindFirstChild("Humanoid") then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if MPS:UserOwnsGamePassAsync(plr.UserId,ID) then
print("ihavepass")
script.Parent.Transparency = 1
script.Parent.CanCollide = false
script.Parent.SurfaceGui.TextLabel.Visible = false
script.Parent.SurfaceGui.TextButton.Visible = false
	end
		end
end)
end
end)
-- serverscript child of click detector
local MPS = game:GetService("MarketplaceService")
local BindableEvent = game.ReplicatedStorage.Event
local GamepassID = 10169496

script.Parent.MouseClick:Connect(function(Player)
   MPS:PromptGamePassPurchase(Player, GamepassID)
end)

MPS.PromptGamePassPurchaseFinished:Connect(function(Player, Id, Purchased)
   if Id == GamepassID and Purchased then
      BindableEvent.Fire(Id) --So you can hook up multiple gamepasses with it
   end
end)

Thanks, have a great day!

1 Like

For me, I would fire off a remote event to the client if they own the gamepass. This same remote event will be used for both checking when the player joins and when they buy it:

--First check when they join
local MPS = game:GetService("MarketplaceService")
local ID == 10169496
local remote = game.ReplicatedStorage.RemoteEvent --name that properly

game.Players.PlayerAdded:Connect(function(player)
    if MPS:UserOwnsGamePassAsync(player.UserId, ID) then
        remote:FireClient(player)
    end
end)

--In a seperate server script, you will fire the remote event when the player buys the VIP
local MPS = game:GetService("MarketplaceService")
local ID == 10169496
local remote = game.ReplicatedStorage.RemoteEvent --name that properly
local button = script.Parent --name that properly


button.MouseClick:Connect(function(player)
   MPS:PromptGamePassPurchase(player, ID)
end)

MPS.PromptGamePassPurchaseFinished:Connect(function(player, gamepassId, purchased)
   if gamepassId == ID and purchased then
      remote:FireClient(player)
   end
end)

--In the local script, you would then change the VIP door for the client
local remote = game.ReplicatedStorage.RemoteEvent --name this properly
local door = workspace.VIPDoor --name this properly

remote.OnClientEvent:Connect(function()
    door.Transparency = 1
    door.CanCollide = false
end)

Let me know if there’s any further issues

1 Like

Where would I put these scripts??

1 Like

You can put the first script in ServerScriptStorage, the second script inside the button, and the third (local) script in StarterGui

1 Like

Also make sure you name all the variables correctly, or else it won’t work

StarterGui??? I’m using a ClickDetector

I said put the local script in StarterGui. The button script goes in the button, and the playeradded script goes in ServerScriptStorage

But I’m not using Gui’s to prompt the gamepass purchase, also is the button, the click detector?

lemme make it more clear:

This goes in ServerScriptStorage:

--First check when they join
local MPS = game:GetService("MarketplaceService")
local ID == 10169496
local remote = game.ReplicatedStorage.RemoteEvent --name that properly

game.Players.PlayerAdded:Connect(function(player)
    if MPS:UserOwnsGamePassAsync(player.UserId, ID) then
        remote:FireClient(player)
    end
end)

This goes inside the button:

--In a seperate server script, you will fire the remote event when the player buys the VIP
local MPS = game:GetService("MarketplaceService")
local ID == 10169496
local remote = game.ReplicatedStorage.RemoteEvent --name that properly
local button = script.Parent --name that properly


button.MouseClick:Connect(function(player)
   MPS:PromptGamePassPurchase(player, ID)
end)

MPS.PromptGamePassPurchaseFinished:Connect(function(player, gamepassId, purchased)
   if gamepassId == ID and purchased then
      remote:FireClient(player)
   end
end)

This goes in StarterGui (ANYWHERE BUT WORKSPACE, IT’S A LOCALSCRIPT):

--In the local script, you would then change the VIP door for the client
local remote = game.ReplicatedStorage.RemoteEvent --name this properly
local door = workspace.VIPDoor --name this properly

remote.OnClientEvent:Connect(function()
    door.Transparency = 1
    door.CanCollide = false
end)

I only said StarterGui because it’s not the workspace (local scripts don’t work in the workspace)

1 Like

Thanks you so much! (30 charsss)

The code mentioned above should fix the problem just fine.
But it’s also good to know what was happening in your original code, and why it didn’t work. So here’s a bit on that:

  • In the VIP door script, you have a connection to the bindable event. And inside that connection you connect an event listener to when the door is touched. The latter connection containing the code which checks if a player has the gamepass and opens the door.
    • Because of the nested nature, the script will only listen for touched events on the door after the bindableEvent has fired once.
      • You’ll also see that the only time you ever fire this is if the gamepass is purchased directly in game firing the PromptGamePassPurchaseFinished listener of the other script. Meaning that the only way the door will open is if the player buys the gamepass in game and then touches the door.
  • You also never revert the door’s openness. And since these scripts occur serverside, if one vip somehow is able to open the door, then all players will be able to go through the door.
2 Likes

You may want to mark this as a solution so people know it’s been solved