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.
-
**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 -
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. -
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