Good script to detect if a player have a gamepass?

Hello, I made this script but I would like if this scipt work because I don’t want rip off player accidentally. Please help me. The GamePass is here if you want see his ID.

local MarketPlaceService = game:GetService("MarketplaceService")
local gamePassID = 13637136

wait(0.0625)
local Player = game.Players.LocalPlayer
local hasPass = false

local success, message = pcall(function()
    hasPass = MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, gamePassID)
end)

if hasPass == true then
    --The player have the gamepass
    game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 32
    game.Players.LocalPlayer.PlayerGui.ScreenGui.SpeedMultiplier.Value = 2
else
    --The player don't have the gamepass
    game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
    game.Players.LocalPlayer.PlayerGui.ScreenGui.SpeedMultiplier.Value = 1
end
1 Like

You should do this on a server script and not on a local script.

3 Likes

this code should be in a regular Script.
and you can’t use game.Player.LocalPlayer in a regular Script.

so your code should be like this :

local MarketPlaceService = game:GetService("MarketplaceService")
local gamePassID = 13637136

game.Players.PlayerAdded:Connect(function(Player)
  wait(0.0625)
  local hasPass = false

  local success, message = pcall(function()
      hasPass = MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, 
  gamePassID)
  end)

  if hasPass == true then
      --The player have the gamepass
      game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 32
      game.Players.LocalPlayer.PlayerGui.ScreenGui.SpeedMultiplier.Value = 2
  else
      --The player don't have the gamepass
      game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
      game.Players.LocalPlayer.PlayerGui.ScreenGui.SpeedMultiplier.Value = 1
  end
end)

have a nice day :herb: .

2 Likes

I see the console

This says:

have you AllowThirdPartySales enabled?

Make sure to is the server checking this rather than the client (change this to a regular script). You can’t use game.Players.LocalPlayer in a regular script.

Allow third party sales only affects you when the asset is made by another user.


I created a button in my game to get a model Created by me and it dont worked until i allowed it

It also should if its owned by a group with a group holder.

I enabled AllowThirdPartySales option on workspace. What this option mean?

It possible to place a boolean value on players to avoid the game have to ckeck several times
if the plauer have a gamepass? (If the player have the gamepass, the boolean must be true else the boolean must be false.)

Here you go it checks if a player owns the game pass when they join and checks if the game pass is purchased in-game if the player owns the game pass it sets up an event to give them the walk speed every time their character resets.

Oh I almost forgot put this in a server script in serverscriptservice as its all server side, enjoy!

local MarketPlaceService = game:GetService("MarketplaceService")
local gamePassID = 13637136
local retrytimeout = 5 --the amount of retries after a failed marketplacefunction before the script will continue

local cache = {} --caches if a player owns the gamepass in the format [Player] = boolean

local function setuprespawn(Player)
	Player.CharacterAdded:Connect(function(Char) --the Player.Character is passed as the first argument
		Char.Humanoid.WalkSpeed = 32
	end)
end

local function applygamepass(Player,HasPass)
	local Character = Player.Character or Player.CharacterAdded:wait()
	if HasPass == true then
		if not cache[Player] then cache[Player] = true end
		print("The player does have the gamepass")
		Character.Humanoid.WalkSpeed = 32 --Applies the walkspeed to the current character
		setuprespawn(Player) --sets up an event to apply the walkspeed to the character everytime they respawn
		Player.PlayerGui.ScreenGui.SpeedMultiplier.Value = 2
	else
		print("The player doesn't have the gamepass")
		--The player don't have the gamepass
		Character.Humanoid.WalkSpeed = 16
		Player.PlayerGui.ScreenGui.SpeedMultiplier.Value = 1
	end
end

local function checkpass(Player)
	local success,HasPass,attempt = nil,nil,0
	repeat
		success,HasPass = pcall(function()
			return MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, gamePassID) --returns a boolean which indicates if the player owns the gamepass or not
		end)
		attempt = attempt + 1
	until (success or attempt >= retrytimeout) --repeats this function until a successful result or until it hits the timeout
	applygamepass(Player,HasPass)
end

game.Players.PlayerAdded:Connect(checkpass)

MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function(...) --this event fires when a purchase dialogue of a game pass is closed so if a player buys the gamepass in game it will invoke this function
	local data = {...} --a table with the arguments passed 
	local Player : Player = data[1] --the player object which recieved the prompt
	local gamepassid : number = data[2] --signed 64bit integer
	local HasPass : boolean = data[3] --a boolean if the player purchased the gamepass or not
	applygamepass(Player,HasPass)
end)

When you dont have AllowThirdPartySales option you can’t sell Gamepasses or models

Also when enabling it in workspace it won’t do anything. I’ve tested that. You need to do it under the security tab.