-- listeners

local function onProductPurchased(player, id)
	local product, class = getAsset(id)

	if product then
		local data = Datastore:GetData(player)
		if product.onPurchased then
			product.onPurchased(player)
		end

		if class == "Passes" and data then
			local gamepasses = data.Gamepasses or {}
			table.insert(gamepasses, product.Id)
			data.Gamepasses = gamepasses
		end
	end	
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, id, success)
	if (success and player.Parent) then
		onProductPurchased(player, id)
		game.ReplicatedStorage.RE.gamepassUpdate:FireClient(player, getNameFromID(id))
	end
end)

MarketplaceService.PromptProductPurchaseFinished:Connect(function(userid, id, success)
	local player = game.Players:GetPlayerByUserId(userid)
	if (success and player and player.Parent) then
		onProductPurchased(player, id)
	end
end)


