How to script multiple gamepasses with multiple items

I know how to script gamepasses but I just don’t know how to make multiple of them that each gives different items.

You would use a for loop to iterate over each item, I would Recommend setting up Attributes to assign a Id for each item

This is an example involving TextButtons:

for number, index in Ex:GetChildren() do -- iterates through items
    index.Activated:Connect(function() -- sets up a connection
        MarketPlaceService:PromptGamePassPurchase(index:GetAttribute("Gamepass")) -- prompts purchase
    end)
end

just do smth like this:

local function gamePassManager(player: Player)
	if game.MarketplaceService:UserOwnsGamePassAsync(player.UserId, 00) then -- replace 00 with ur id 
		-- stuff
	elseif game.MarketplaceService:UserOwnsGamePassAsync(player.UserId, 000) then -- replace 00 with ur other idid 
		
		-- stuff
	end
end

game.Players.PlayerAdded:Connect(gamePassManager)

modularly

local gamepassFunctions = require(script.gamepasses)

local players = game:GetService("Players")
local marketPlaceService
players.PlayerAdded:Connect(function(plr)
   for gamepassId, gamepassFunction in gamepassFunction do
      if marketPlaceServcie:UserOwnsGamePassAsync(gamepassId) then
         gamepassFunction(plr)
      end
   end
end)
--script.gamepasses
return {
    [mygamepassid] = function(plr : Player)
       --code
    end,
    [myothergamepass] = function(plr : Player)
       --code
    end
}

where code is the item giver.

1 Like

Where it says “gamepass” I would put the gamepass ID?

Create an Attribute, and add the gamepass there

I would strongly use this, but have it within a Module script.

The areas where it says “–code” is where you would clone the things needed to be given
@eMisakie

I’ll try this out, I’ve been trying to get into module scripts more :smiling_face_with_tear:

for number, item in Ex:GetChildren() do – iterates through items
item.Activated:Connect(function() – sets up a connection
MarketPlaceService:PromptGamePassPurchase(item:GetAttribute(“Gamepass”)) – prompts purchase
end)
end

It works :smiley: thank you, but I was wondering how to use it when someone wants to buy a gamepass? Because I receive my items because I already own the gamepass, but I don’t receive my items when I try to buy it. What should I write for PromptGamepassPurchaseFinished?

Ah. This type of system only checks the gamepass owning whenever a player joined. You would want a signal to tell the server that the player purchased a gamepass.
You can use _G and a signal library.

local gamepassFunctions = require(script.gamepasses)
local signalLib = require(somesignallibrary)
_G.gamepassSignal = signalLib.new()
local players = game:GetService("Players")
local marketPlaceService = game:GetService("MarketplaceService")
local function gamepassChecker(plr: Player, id: number)
   if id then
      if marketPlaceService:UserOwnsGamePassAsync(id) then
         gamepassFunctions[id](plr)
      end
      return
   end
   for gamepassId, gamepassFunction in gamepassFunctions do
      if marketPlaceServcie:UserOwnsGamePassAsync(gamepassId) then
         gamepassFunction(plr)
      end
   end
end
players.PlayerAdded:Connect(gamepassChecker)
_G.gamepassSignal:Connect(gamepassChecker)

note this is psuedo code and “signalLib” should be a signal library like fastsignal.

Fire the event by _G.gamepassSignal:Fire(idofgamepass)
in another script to give a gamepass.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.