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

How do i check if if a player has a gamepass?

Asked by 4 years ago

Hi! So I made a gamepass that makes X2 coins but, I want to check if the player has the gamepass but I have to check that in another script here is the script (I used UserOwnsGamePassAsync but did not work)

local mps = game:GetService("MarketplaceService")
local button = script.Parent
local debounce = false
local gamePassID = 9189975

button.Touched:Connect(function(hit)
    debounce = true
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if not player then return end
        if player then
            if player.Team.Name == "Red" then
                if mps:UserOwnsGamePassAsync(player, gamePassID) then
                     player.leaderstats.Money.Value = player.leaderstats.Money.Value + button.Parent.Screen.MoneySaved.Value * 2
                button.Parent.Screen.MoneySaved.Value = 0
                debounce = false
                if debounce == false then
                    button.BrickColor = BrickColor.new("Really red")
                    wait(2)
                    button.BrickColor = BrickColor.new("Sea green")
                end
                end
            end
        end
    end
end)
0
I suggest using user.Id instead of player because the player name can change but the userid can not. JesseSong 3916 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

I dont use UserOwnsGamePassAsync, what I use is

if mps:PlayerOwnsAsset(player, gamePassID) then

Also, you said

 if player.Team.Name == "Red" then
               if mps:PlayerOwnsAsset(player, gamePassID) then

Instead Of

  if player.Team.Name == "Red" and
                mps:PlayerOwnsAsset(player, gamePassID) then

Hopefully that helps, completed code using that method is

local mps = game:GetService("MarketplaceService")
local button = script.Parent
local debounce = false
local gamePassID = 9189975

button.Touched:Connect(function(hit)
    debounce = true
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if not player then return end
        if player then
            if player.Team.Name == "Red" and
                mps:PlayerOwnsAsset(player, gamePassID) then
                     player.leaderstats.Money.Value = player.leaderstats.Money.Value + button.Parent.Screen.MoneySaved.Value * 2
                button.Parent.Screen.MoneySaved.Value = 0
                debounce = false
                if debounce == false then
                    button.BrickColor = BrickColor.new("Really red")
                    wait(2)
                    button.BrickColor = BrickColor.new("Sea green")
                end
                end
            end
        end
    end)
0
Did not work doesn't print anything and only stops at PlayerOwnsAsset Baselistic 103 — 4y
0
Hmm Explorer2021 22 — 4y
Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

The built in function :UserOwnGamePassAsync is a way to show it a player has a gamepass.

Make sure it's in a local script Also if you want to grant your self something like a tool you must need a remote event since the local player cannot give tools to its back pack

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

-- Function to prompt purchase of the game pass
local function promptPurchase()

    local player = Players.LocalPlayer
    local hasPass = false

    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID) -- checks to see if a player has a gamepass if not it will error 


button.Touched:Connect(function(hit)
    debounce = true
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if not player then return end
        if player then
            if player.Team.Name == "Red" then
                if hasPass then
                     player.leaderstats.Money.Value = player.leaderstats.Money.Value + button.Parent.Screen.MoneySaved.Value * 2
                button.Parent.Screen.MoneySaved.Value = 0
                debounce = false
                if debounce == false then
                    button.BrickColor = BrickColor.new("Really red")
                    wait(2)
                    button.BrickColor = BrickColor.new("Sea green")
                end
                end
            end
        end
    end
end)
    end)

    if not success then
        warn("Error while checking if player has pass: " .. tostring(message))
        return
    end

    if hasPass then
        end
end
end
end
end
0
But my script is in a server script. Do i just put it in a local script instead? Baselistic 103 — 4y
0
Yes JesseSong 3916 — 4y
0
Im not trying to prompt anything im just making so it gives double cash if the player has the x2 cash gamepass and not trying to prompt doing this script i already prompted in a gui Baselistic 103 — 4y
0
Ok i will edit it later JesseSong 3916 — 4y
0
Edited JesseSong 3916 — 4y
Log in to vote
0
Answered by 4 years ago
local passId = 0000000 -- change this to your game pass ID.

function isAuthenticated(player) -- checks to see if the player owns your pass 
    return game:GetService("GamePassService"):PlayerHasPass(player, passId) -- this method will check to see if player has the pass from the passId
end

function Start(plr)
    print(plr.Name .. " has bought the game pass with id " .. passId)
end

game.Players.PlayerAdded:connect(function(plr)
    if isAuthenticated(plr) then -- checks to see if player is authenticaded
        Start(plr) -- activate function
    end
end)

This is the code it's really easy

0
put it in a local scrpit TheInternetMemer 40 — 4y

Answer this question