Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

1 comment

0

I suggest using user.Id instead of player because the player name can change but the userid can not.

3 answers

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)

2 comments

0

Did not work doesn't print anything and only stops at PlayerOwnsAsset

0

Hmm

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

5 comments

0

But my script is in a server script. Do i just put it in a local script instead?

0

Yes

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

0

Ok i will edit it later

0

Edited

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

1 comment

0

put it in a local scrpit

This discussion is preserved as it was. New answers, comments, and votes are closed.