VIP detector not working

I am working on a randomizer game and am trying to assign VIP players an extra slot of weapons which are categorized in folders in replicated storage. The other assignment scripts work fine but when I add the VIP detector the script doesn’t work at all.

local gamepassId = 25926544 -- ID of the gamepass
local service = game:GetService("MarketplaceService") --  Buy the gamepass  
game.Players.PlayerAdded:Connect(function(player)
	if (service:UserOwnsGamePassAsync(player.UserId, gamepassId)) then -- If you buy the gamepass playing the game
		local RS = game:GetService("ReplicatedStorage")
		local Players = game:GetService("Players")
		local Folder = RS:WaitForChild("op weapons")
		-- Change the "op" if you change the folder name in Replicated Storage

		local function onCharacterAdded(character)
			local Tools = Folder:GetChildren()
			local ToolsTable = Tools
			local chosenTools = {}

			repeat
				local selectedIndex = math.random(1, #ToolsTable)
				table.insert(chosenTools, ToolsTable[selectedIndex])
				table.remove(ToolsTable, selectedIndex)
			until #chosenTools >= 1
			-- Numbers of tools going to give

			local plr = Players:GetPlayerFromCharacter(character)
			for i,v in pairs(chosenTools) do
				Folder:FindFirstChild(v.Name):Clone().Parent = plr.Backpack
			end
		end

		local function onAdded(player)
			player.CharacterAdded:Connect(onCharacterAdded) -- Triggers when the player spawns or respawns
		end

		Players.PlayerAdded:Connect(onAdded)
		--HOW TO USE
		--put this script in server script service
		--put a folder in ReplicatedStorage
		--and take all of the tools you want to have a chance to be given just put them in that new folder 
		--and your done!
	end

any help would be greatly appreciated :slight_smile:

You’re connecting the PlayerAdded function inside the PlayerAdded function, it won’t work. You can just remove the extra onAdded function and just write player.CharacterAdded:Connect(onCharacterAdded)

where exactly do you mean

local function onAdded(player)
player.CharacterAdded:Connect(onCharacterAdded) – Triggers when the player spawns or respawns
end

or

Players.PlayerAdded:Connect(onAdded)

local gamepassId = 25926544 -- ID of the gamepass
local service = game:GetService("MarketplaceService") --  Buy the gamepass  
game.Players.PlayerAdded:Connect(function(player)
	if (service:UserOwnsGamePassAsync(player.UserId, gamepassId)) then -- If you buy the gamepass playing the game
		local RS = game:GetService("ReplicatedStorage")
		local Players = game:GetService("Players")
		local Folder = RS:WaitForChild("op weapons")
		-- Change the "op" if you change the folder name in Replicated Storage

		local function onCharacterAdded(character)
			local Tools = Folder:GetChildren()
			local ToolsTable = Tools
			local chosenTools = {}

			repeat
				local selectedIndex = math.random(1, #ToolsTable)
				table.insert(chosenTools, ToolsTable[selectedIndex])
				table.remove(ToolsTable, selectedIndex)
			until #chosenTools >= 1
			-- Numbers of tools going to give

			local plr = Players:GetPlayerFromCharacter(character)
			for i,v in pairs(chosenTools) do
				Folder:FindFirstChild(v.Name):Clone().Parent = plr.Backpack
			end
		end

			player.CharacterAdded:Connect(onCharacterAdded) -- Triggers when the player spawns or respawns

		--HOW TO USE
		--put this script in server script service
		--put a folder in ReplicatedStorage
		--and take all of the tools you want to have a chance to be given just put them in that new folder 
		--and your done!
	end
local Players = game:GetService("Players")
local MPS = game:GetService("MarketplaceService")
local RS = game:GetService("ReplicatedStorage")
local Folder = RS:WaitForChild("op weapons")

local gamepassId = 25926544

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local hasGamepass = MPS:UserOwnsGamePassAsync(Player.UserId, gamepassId)
		if hasGamepass then
			local Tools = Folder:GetChildren()
			local randomTool = Tools[math.random(1, #Tools)]
			local randomToolClone = randomTool:Clone()
			randomToolClone.Parent = Player.Backpack
		end
	end)
end)

I’m guessing you were using a tutorial script, it isn’t the most efficient, I’ve cleaned it up a lot.

1 Like

thanks! I worked off of a tutorial script yes