Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make this guI visible when a player owns a gamepass?

Asked by 5 years ago

I just want to make a script where a gui is visible when a player owns a gamepass. Also my game is filtering enabled and this is a local script.

local id = 4894476
local player = game.Players.LocalPlayer

if game:GetService("MarketplaceService"):PlayerOwnsAsset(player, id) then
    script.Parent.Visible = true
else
    script.Parent.Visible = false
end

Can someone please help me figure out what is wrong?

Thank you.

0
Do if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, id) then User#19524 175 — 5y
0
Thanks I will try that. User#23357 0 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

PlayerOwnsAsset doesn't work for Gamepasses; use UserOwnsGamepassAsync. Heres a better script made by my friend Vissequ with your variables inserted into it:

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

local gamePassID = 4894476-- Change this to your game pass ID 

function onPlayerSpawned(player) 

    local hasPass = false

    -- Check if the player already owns the game pass
    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.userId, gamePassID)
        print("Has Game Pass")
    end)

    -- If there's an error, issue a warning and exit the function
    if not success then
        warn("Error while checking if player has pass: " .. tostring(message))
        return
    end

    if hasPass == true then
        script.Parent.Visible = true
else
    script.Parent.Visible = false
    end
end 

game.Players.PlayerAdded:Connect(function(player)
  player.CharacterAdded:Connect(function()
    onPlayerSpawned(player)
  end)
end)

-- Connect 'PlayerAdded' events to the 'onPlayerAdded()' function
Players.PlayerAdded:Connect(onPlayerSpawned)
--Script made by Vissequ
Ad

Answer this question