Gamepass Characters with Character Selection Gui

Hello all!
First of all, I’m new to scripting and game design however I do a lot of self-research for most answers however I have a puzzle with multiple scripts which I’m hoping we can piece together.

  1. **What do you want to achieve?
    I’m creating characters that can be purchased and played as via game pass and I want it so that the screen GUI has a character selection with an option to equip or buy the characters

  2. What is the issue?
    The issue is that I have multiple scripts that achieve some of the desired results however I’m not sure of how to combine them. Also if a player has multiple games pass characters it just overwrites each one until it gets to the last purchased one and uses that.

  3. What solutions have you tried so far?
    I have researched multiple videos and forums posts with similar ideas but not quite the same and I just can’t piece the scripts together.

Below is the character selection screen that works however it doesn’t use a game pass at all

local Player = script.Parent.Parent.Parent
local Content = script.Parent.MainFrame.ScrollingFrame.Content
local Characters = game.ReplicatedStorage.Characters
local SetSubject = Characters.Parent.SetSubject

for index,item in pairs(Characters:GetChildren()) do
	if item:FindFirstChild("Humanoid") then
		local ViewportFrame = Instance.new("ViewportFrame")
		ViewportFrame.Parent = Content
		ViewportFrame.BackgroundTransparency = 1

		local Button = Instance.new("TextButton")
		Button.Parent = ViewportFrame
		Button.Position = UDim2.new(0,0,1,0)
		Button.Size = UDim2.new(1,0,0,15)
		Button.BorderSizePixel = 0
		Button.BackgroundColor3 = Color3.fromRGB(255,255,255)
		Button.Text = "Play As"
		Button.TextScaled = true

		local Preview = item:Clone()
		Preview.Parent = ViewportFrame

		local Camera = Instance.new("Camera")
		Camera.Parent = ViewportFrame
		Camera.CFrame = Preview.Head.CFrame + Preview.Head.CFrame.LookVector * 5
		Camera.CFrame = CFrame.new(Camera.CFrame.Position,Preview.Head.Position)

		ViewportFrame.CurrentCamera = Camera

		Button.MouseButton1Down:Connect(function()
			script.Parent.Enabled = false
			local ChosenCharacter = item:Clone()
			local CurrentCharacter = Player.Character
			local LocalScripts = {}

			for index2,item2 in pairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do
				if item2:IsA('LocalScript') then
					table.insert(LocalScripts,item2:Clone())
				else
					item2:Clone().Parent = ChosenCharacter
				end
			end

			CurrentCharacter.Health:Clone().Parent = ChosenCharacter
			table.insert(LocalScripts,CurrentCharacter.Animate:Clone())     
			ChosenCharacter.Parent = workspace
			Player.Character = ChosenCharacter
			for index2,item2 in pairs(LocalScripts) do
				item2.Parent = ChosenCharacter
			end 
			SetSubject:FireClient(Player,ChosenCharacter.Humanoid)

			local Connection

			local function onDied()
				wait(game.Players.RespawnTime)
				Player:LoadCharacter()
				script.Parent.Enabled = true
				if Connection then
					Connection:Disconnect()
				end
			end

			Connection = ChosenCharacter.Humanoid.Died:Connect(onDied)

		end)

	end
end

Next is the game pass script which works separately however it only allows one purchase to be applied and it’s always the last one.

local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassId = 0000000 -- changed for privacy

game.Players.PlayerAdded:Connect(function(player)
	print("Player added")
	player.CharacterAdded:Wait()
	print("Character added")
	
	if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassId) then 
		print("Owns Pass")
		local CharacterClone = game.ReplicatedStorage.Characters:FindFirstChild("NPC"):Clone() -- change name of child
		CharacterClone.Name = player.Name
		player.Character:Destroy()
		player.Character = CharacterClone
		CharacterClone.Parent = game.Workspace
	end
end)

I suspect I have to change the button code somehow but not sure how so any help will be appreciated
image

2 Likes

You have to iterate over all of the returned passes and process each one
Define all your game passes in a single dictionary:

-- VARIABLES
local gamePasses = {}
gamePasses["First Game Pass"] = 11111111111 
gamePasses["Second GP"] = 22222222222
gamePasses["Third GP"] = 333333333

game.Players.PlayerAdded:Connect(function(player)
-- Loop through GamePasses Table	
	for index, passID in pairs(gamePasses) do
		local hasPass = false
		-- Check if the player already owns the game pass
		local success, message = pcall(function()
			hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, passID)
		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
-- CHECK IF PLAYER HAS PASS		
		if hasPass == true then
			print(player.Name.." owns the game pass"..gamePasses[index].." with ID ".. passID)
			-- Assign this player the ability or bonus related to the game pass
			if passID == 11111111111 then
-- PLAYER OWNS First Game Pass		
				print("Player owns First Game Pass")
				-- Do stuff for the pass
			end
			if passID == 22222222222 then
-- PLAYER OWNS Second GP			
				print("Player owns Second GP")
				-- Do stuff for the pass
			end
			if passID == 333333333 then
-- PLAYER OWNS Third GP				
				print("Player owns Third GP")
				-- Do stuff for the pass
			end
		end
	end
end
1 Like

Thank you for this, putting all the game passes into a single dictionary does helps keep the number of scripts needed to a minimum and will be great for items/tools or even weapons. I will be using this method for another game.

However, I still have the original issue for the game pass items overwriting each other.
I believe this is because I’m assigning a new starter character each time on player connect. Therefore the last purchase is the one being assigned. This is correct as I only want them to play a single starter character at a time but still allow them to choose which character to spawn in as.

For example, the issue right now is if I have a playable starter character named Bob and I script it so if you own Bob game pass then your starter character will be Bob and it will replace your personal Roblox avatar.
However, if you own Bob and another game pass starter character named Dan then when you load into the game, your Roblox avatar will be replaced by Bob then replaced again by Dan

I have been testing a bunch of game pass shop tutorials however I still can’t get the desired result.

Right now I’m trying to design a shop GUI with each button checking a different game pass id but struggling to get it all working.
Perhaps if I can find an equip character tutorial somewhere it might help but so far none I found are for game pass characters

OK. Understood. I use the ProfileService Datastore Module to save the players equipped items like weapons and pets when they leave the game, so I can restore them when they return. It’s a fairly easy module to use:

I used this Vid as a tutorial for it:
Intro to ProfileService

That is a great video for managing data and I have bookmarked it for a future game.
Staying on topic though I was able to get the script mostly working as I just needed to keep it simple,
I created a local script for a button and did the game pass checks/replace character code in the script.

The sources that helped were:

https://education.roblox.com/en-us/resources/adventure-game-monetization-game-passes