I am trying to make two game passes give you two separate gears. The script works sometimes but oftentimes only gives me the “Speed” gear.
local GamePass = game:GetService("MarketplaceService")
game:GetService("Players").PlayerAdded:Connect(function(plr)
if GamePass:UserOwnsGamePassAsync(plr.UserId,9146292) then
local tool = game:GetService("ServerStorage"):FindFirstChild("Gravity"):Clone()
tool.Parent = plr:WaitForChild("Backpack")
end
if GamePass:UserOwnsGamePassAsync(plr.UserId,9146295) then
local tool = game:GetService("ServerStorage"):FindFirstChild("Speed"):Clone()
tool.Parent = plr:WaitForChild("Backpack")
end
end)
I wrote this script with the help of the Roblox Wiki, but in the example they gave it was only using one game pass. (The Script is in ServerScriptService)
This is the game-pass gear scrip I use. It should work, never had an issue with it.
--------------------
--| WaitForChild |--
--------------------
-- Waits for parent.child to exist, then returns it
local function WaitForChild(parent, childName)
assert(parent, "ERROR: WaitForChild: parent is nil")
while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
return parent[childName]
end
-----------------
--| Variables |--
-----------------
local GamePassService = Game:GetService('GamePassService')
local PlayersService = Game:GetService('Players')
local InsertService = Game:GetService('InsertService')
local LightingService = Game:GetService('Lighting') --TODO: Use new data store service once that exists
local GamePassIdObject = WaitForChild(script, 'GamePassId')
local ToolAssetsToLoad = WaitForChild(script, 'ToolAssetsToLoad')
local AdminTools = LightingService:FindFirstChild('AdminTools')
-----------------
--| Functions |--
-----------------
-- Makes copies of all the admin tools and puts them in target
local function CloneAdminTools(target)
for _, tool in pairs(AdminTools:GetChildren()) do
local toolClone = tool:Clone()
toolClone.Parent = target
end
end
-- When a player with the game pass joins, give them the admin tools
local function OnPlayerAdded(player)
if GamePassService:PlayerHasPass(player, GamePassIdObject.Value) then
local starterGear = WaitForChild(player, 'StarterGear')
CloneAdminTools(starterGear)
if player.Character then -- They've already loaded and won't get their StarterGear until next spawn
local backpack = WaitForChild(player, 'Backpack')
CloneAdminTools(backpack)
end
end
end
--------------------
--| Script Logic |--
--------------------
-- Create AdminTools if it doesn't exist
if not AdminTools then
AdminTools = Instance.new('Model')
AdminTools.Name = 'AdminTools'
-- Load all of the assets in ToolAssetsToLoad and put them in AdminTools
for _, intObject in pairs(ToolAssetsToLoad:GetChildren()) do
if intObject and intObject:IsA('IntValue') and intObject.Value then
local assetModel = InsertService:LoadAsset(intObject.Value)
if assetModel then
local asset = assetModel:GetChildren()[1]
if asset then
asset.Parent = AdminTools
end
end
end
end
AdminTools.Parent = LightingService
end
PlayersService.PlayerAdded:connect(OnPlayerAdded)
It’s a normal script and put an IntValue with the gamepass ID in it. Then a group called “ToolAssetsToLoad” and another int value with the tool ID.
local GamePass = game:GetService("MarketplaceService")
game:GetService("Players").PlayerAdded:Connect(function(plr)
if GamePass:UserOwnsGamePassAsync(plr.UserId,9146292) then
local tool = game:GetService("ServerStorage"):FindFirstChild("Gravity"):Clone()
tool.Parent = plr:WaitForChild("Backpack") .. ("StarterGear")
end
if GamePass:UserOwnsGamePassAsync(plr.UserId,9146295) then
local tool = game:GetService("ServerStorage"):FindFirstChild("Speed"):Clone()
tool.Parent = plr:WaitForChild("Backpack") .. ("StarterGear")
end
end)
You can duplicate this by doing command d and fill the info out again at least that’s what I did. Plus it’d be easier to remove any game-passes in the future instead of having to select lines of code to delete and rewriting it for a new amount.
StarterGear is underneath every player, and will give it to that one player every spawn.
I see three potential issues here:
If the PlayerAdded event is connected after the player joins, it won’t run that code;
If the Player’s character hasn’t loaded when you add the gamepass to their backpack it won’t be added; and
If the player dies, the gamepass will leave their inventory.
The solution is to clone it into both StarterPack and StarterGear.
local GamePass = game:GetService("MarketplaceService")
game:GetService("Players").PlayerAdded:Connect(function(plr)
if GamePass:UserOwnsGamePassAsync(plr.UserId,9146292) then
game:GetService("ServerStorage"):FindFirstChild("Gravity"):Clone().Parent = plr:WaitForChild("Backpack")
game:GetService("ServerStorage"):FindFirstChild("Gravity"):Clone().Parent = plr:WaitForChild("StarterGear")
end
if GamePass:UserOwnsGamePassAsync(plr.UserId,9146295) then
game:GetService("ServerStorage"):FindFirstChild("Speed"):Clone().Parent = plr:WaitForChild("Backpack")
game:GetService("ServerStorage"):FindFirstChild("Speed"):Clone().Parent = plr:WaitForChild("StarterGear")
end
end)```