Check backpack click

I am trying to make it so that when the player clicks to equip the backpack tool, it checks if they have that item’s gamepass, but it’s not working.

Like this game:

I tryed this code:

local function handleTool(tool)
	if tool:IsA("Tool") then
		print("Conectando Equipped na tool:", tool.Name)  
		tool.Equipped:Connect(function()
			print("Tool equipada: " .. tool.Name) 
			local ToolId = MarketIds.Gamepasses[tool.Name]
			if not ToolId then return end 

			local hasPass = false
			pcall(function()
				hasPass = MarketplaceService:UserOwnsGamePassAsync(LocalPlayer.UserId, ToolId)
			end)

			if not hasPass then
				tool.Parent = nil
				MarketplaceService:PromptGamePassPurchase(LocalPlayer, ToolId)
			end
		end)
	end
end

for _, tool in ipairs(backpack:GetChildren()) do
	handleTool(tool)
end

backpack.ChildAdded:Connect(handleTool)

MarketplaceService:UserOwnsGamePassAsync must be called from a server script.
LocalPlayer and PromptGamePassPurchase must run on a client script.

Basic Server-Side Ownership Check with Client Purchase Prompt.

Client
task.wait(1)
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchasePrompt = ReplicatedStorage:WaitForChild("PurchasePrompt")

PurchasePrompt.OnClientEvent:Connect(function(passId)
	MarketplaceService:PromptGamePassPurchase(game.Players.LocalPlayer, passId)
end)
Server
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local PurchasePrompt = Instance.new("RemoteEvent", ReplicatedStorage)
PurchasePrompt.Name = "PurchasePrompt" --or create this, then define here

Players.PlayerAdded:Connect(function(player)
	player.Backpack.ChildAdded:Connect(function(tool)
		if tool:IsA("Tool") then
			tool.Equipped:Connect(function()
				local id = MarketIds.Gamepasses[tool.Name]
				if not id then return end
				local hasPass = false
				pcall(function()
					hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, id)
				end)
				if not hasPass then
					tool:Destroy()
					PurchasePrompt:FireClient(player, id)
				end
			end)
		end
	end)
end)
1 Like

just do:

local try,hasPass = pcall(MarketplaceService.UserOwnsGamePassAsync,MarketplaceService,LocalPlayer.UserId,ToolId)


Also im pretty sure you want to make it server sided.

1 Like

Set ManualActivationOnly to true if they do not own it and false if they do own it.
This would disallow players from equipping it and then clicking/activating it.
Though if your code runs on .Equipped or .Unequipped you’ll need to figure out different code for that. Which you’d need to put in every single tool, just check if ManualActivationOnly is set to false, if it is not then return.

1 Like