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)